具有灵活高度的ListView中的Android自定义行布局

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

您好,我正在尝试创建这样的行: enter image description here

行应该具有灵活的高度并且随着字幕文本的增加而变高。 左边的绿色 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>

但这是我得到的结果:

enter image description here

很明显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/

相关文章:

Android对话框 Activity 在外部触摸时删除边距并完成

java - 屏幕尺寸不同? (景观)

android - 就我而言,如何将一种布局嵌入另一种布局?

java - ListView 不响应 Android 中的点击事件

android - 自定义微调器不调用自定义 ListView ?

java - 解析包数据

Android ActionBar 兼容溢出菜单未在 sdk 10 上显示

Android:按下 ListView 项目中的按钮

java - 获取对象的嵌套数组,改造android

android - 想要在 android 中为角色设置动画