【问题标题】:Android: Adding a divider into a list item with dynamic heightAndroid:将分隔线添加到具有动态高度的列表项中
【发布时间】:2014-12-10 18:13:55
【问题描述】:

我目前正在尝试创建一个类似于the official design guidelines 中的列表项。仍然缺少的是每个项目右侧的按钮/图像旁边的垂直分隔线。大多数人似乎为此使用具有固定宽度的单独视图,但在我的情况下高度没有缩放。它仅在我为分隔线本身或父级 RelativeLayout 设置固定高度时显示。是否可以让它自动扩展?

列表项的布局 XML 如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/sensors_list_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="10dp" />
    <TextView
        android:id="@+id/sensors_list_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_toRightOf="@id/sensors_list_img"
        android:paddingTop="5dp" />
    <TextView
        android:id="@+id/sensors_list_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:layout_below="@id/sensors_list_name"
        android:layout_toRightOf="@id/sensors_list_img"
        android:paddingBottom="5dp" />
    <ImageView
        android:id="@+id/sensors_list_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:padding="10dp"
        android:src="@drawable/ic_angle_right" />
    <View
        android:id="@+id/sensors_list_divider"
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/sensors_list_details"
        android:background="#ff0000" />
</RelativeLayout>

【问题讨论】:

  • 你说的图像视图缩放。看起来不是。它匹配父高度而不是包装内容。可能是秤里面的内容。
  • 你是对的。我这样做是为了让他们可以集中他们的内容,但这不是必需的。我将图像的宽度和高度更改为 wrap_content。但是,主要问题仍然存在:单个 View 没有扩展它的高度以匹配其父级的高度。
  • 请检查新的更新并告诉我结果。

标签: android android-layout


【解决方案1】:

尝试分配与图像相关的高度,例如:

android:alignTop="@id/myimage"
android:alignBottom="@id/myimage"

否则,您可以创建一个 xml 可绘制对象并将其与 ImageView 一起使用:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
   android:shape="line">
    <stroke android:width="1dp" android:color="#000000"/>
</shape>

更新 记住android:shape="line" 没有stroke 就无法工作

【讨论】:

  • 使用 alignTop 和 alignBottom 并没有太大变化,特别是因为 layout_height 仍然是必需的。我用 wrap_content 和 match_parent 都试过了,但在这两种情况下都看不到这条线。我会给可绘制的 ImageView 一个镜头。无论如何,视图的高度与父级的高度不匹配的原因是什么?有很多解决方法,但我对所有这些背后的推理特别感兴趣。
【解决方案2】:

让我们以LinearLayout 作为父母:

<?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">

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/sensors_list_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:paddingTop="5dp" />
        <TextView
            android:id="@+id/sensors_list_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:paddingBottom="5dp" />

    </LinearLayout>

    <View
        android:id="@+id/sensors_list_divider"
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#ff0000" />

    <ImageView
        android:id="@+id/sensors_list_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:src="@drawable/ic_angle_right" />

</LinearLayout>

【讨论】:

  • 对不起,为了在我的示例中简洁起见,我删除了两个 TextView 都引用的图像。我刚刚将其重新编辑。除此之外,您的示例没有显示细红线。这正是我的问题:单个“视图”没有将它的高度扩展到它的父级。
【解决方案3】:
Check this 
listView.setDividerHeight(1);

【讨论】:

  • 这不是他想要的。他想要在项目内有一个分隔线。划分图像和其他项目。
  • 问题是关于列表项内的垂直分隔线,而不是列表项之间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 2011-01-28
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多