您好,我正在尝试创建这样的行:
行应该具有灵活的高度并且随着字幕文本的增加而变高。 左边的绿色 View 也应该增长以填满行的整个高度。 行高应仅根据文本(标题 + 副标题)确定。
这是我创建的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<View
android:id="@+id/is_in_range_view"
android:layout_width="15dip"
android:layout_height="fill_parent"
android:background="#99CC00" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/row_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:text="@string/title"
android:textSize="16sp" />
<TextView
android:id="@+id/row_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="10dip"
android:text="@string/subtitle"
android:textSize="12sp" />
<View
style="@style/SeparatorLine"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="5dip" />
</LinearLayout>
</LinearLayout>
但这是我得到的结果:
很明显View的layout_height属性的fill_parent
值是有问题的,但这正是我想要的!
我只希望行高基于文本,而不是绿色 View 。
我该怎么做?
最佳答案
好的,问题是我在包含两个 TextView
的内部 LinearLayout
中为 layout_height
设置了错误的值。
它被设置为 fill_parent
而不是 wrap_content
。
设置为 wrap_content
后一切正常。
正确的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<View
android:id="@+id/is_in_range_view"
android:layout_width="15dip"
android:layout_height="fill_parent"
android:background="#99CC00" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/row_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:maxLines="1"
android:text="@string/title"
android:textSize="16sp" />
<TextView
android:id="@+id/row_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="10dip"
android:maxLines="3"
android:text="@string/subtitle"
android:textSize="12sp" />
<View
style="@style/SeparatorLine"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="5dip" />
</LinearLayout>
关于具有灵活高度的ListView中的Android自定义行布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10558073/