【问题标题】:Android custom row layout in ListView with flexible heightListView中具有灵活高度的Android自定义行布局
【发布时间】:2012-05-11 20:18:25
【问题描述】:

您好,我正在尝试创建这样的行:

该行应具有灵活的高度,并且随着字幕文本的长度变高。 左侧的绿色视图也应该增长以填满行的整个高度。 行高应仅根据文本(标题+副标题)确定。

这是我创建的布局:

<?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 值是有问题的,但这正是我想要的! 我只希望行高基于文本,而不是绿色视图。

我该怎么做?

【问题讨论】:

  • 为什么会有最外层的LinearLayout?这似乎是多余的,因为它唯一的孩子是另一个 LinearLayout。
  • @Sam sorty 你是对的。固定的。但它仍然没有解决它虽然

标签: android android-layout android-listview android-linearlayout android-view


【解决方案1】:

好的,问题是我在包含两个TextView's 的内部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>

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多