【问题标题】:Use a vertical line in a ListView item在 ListView 项中使用垂直线
【发布时间】:2013-11-20 20:22:01
【问题描述】:

我正在尝试在 android ListView 项目中显示一条垂直线。我在单元格的 XML 中定义它,但是当我在 ListView 中预览它时它不会出现。

我知道这是一个问题,因为我在这里看到了很多问题,但没有看到任何真正的答案。

XML 非常标准:

单元格:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/appWhite" >

  <View android:layout_width="5dp"
      android:layout_height="match_parent"
      android:background="@color/appGreen"
      android:layout_alignParentLeft="true" />

  <RelativeLayout
      android:id="@+id/cell_trip_info_container"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/vertical_bar" >

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cell_trip_name"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:textColor="#2c3e50"
        android:paddingLeft="10dp"
        android:text="adfadf" />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cell_trip_country"
        android:layout_below="@+id/cell_trip_name"
        android:textColor="#2c3e50"
        android:paddingLeft="10dp"
        android:text="adfadf" />
  </RelativeLayout>

  <RelativeLayout android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/whole_container"
    android:layout_alignParentRight="true" >

    <RelativeLayout android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:background="@color/appGreen"
      android:id="@+id/cell_trip_date_box"
      android:layout_alignTop="@+id/update_buttons"
      android:layout_alignBottom="@+id/update_buttons"
      android:padding="20dp" >

      <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cell_tripstart_date"
        android:layout_centerHorizontal="true"
        android:textColor="#fff" />
      <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cell_date_thru"
        android:text="@string/thru"
        android:layout_below="@+id/cell_tripstart_date"
        android:layout_centerHorizontal="true"
        android:textColor="#fff" />
      <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cell_tripend_date"
        android:layout_below="@+id/cell_date_thru"
        android:textColor="#fff"
        android:layout_centerHorizontal="true" />

    </RelativeLayout>    

    <RelativeLayout android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:id="@+id/update_buttons"
      android:visibility="gone"
      android:layout_toRightOf="@+id/cell_trip_date_box"
      android:background="#e9e9e9"
      android:padding="20dp" >

      <ImageView android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/cell_button_edit_trip"
        android:src="@drawable/slide_edit"
        android:adjustViewBounds="true"
        android:contentDescription="@string/content_description" />

      <ImageView android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/cell_button_delete_trip"
        android:src="@drawable/slide_delete"
        android:layout_toRightOf="@+id/cell_button_edit_trip"
        android:adjustViewBounds="true"
        android:contentDescription="@string/content_description" />

    </RelativeLayout>   
  </RelativeLayout>
</RelativeLayout>

【问题讨论】:

  • 请包含您的 ListView 项目的所有 XML。

标签: android android-layout android-listview


【解决方案1】:

我在另一篇帖子 Here 上找到了原因和明显的解决方案/解决方法

基本上,无论出于何种原因,RelativeLayouts 都不能很好地与 ListView 项中的“match_parent”高度设置配合使用。该帖子的答案之一建议使用 LinearLayout 代替。所以我刚刚创建了一个新文件,其父视图是 LinearLayout。然后,我创建了一个 RelativeLayout 子项并将其他所有内容都扔在那里。

这次效果很好,很容易让我创建我的垂直规则。

【讨论】:

  • 如此愚蠢但实际上有效。不要忘记将内部的 RelativeLayout layout_height 设置为 match_parent ,否则您将得到相同的结果。
【解决方案2】:

在你的cell_trip_info_container RelativeLayout 你有

android:layout_toRightOf="@+id/vertical_bar"

你没有名为vertical_bar的视图

将该 ID 添加到您的垂直条视图中,您应该可以开始使用了

<View
      android:id="@+id/vertical_bar"
      android:layout_width="5dp"
      android:layout_height="match_parent"
      android:background="@color/appGreen"
      android:layout_alignParentLeft="true"  />

【讨论】:

  • imgur.com/n8XaatK.jpg 是我使用您提供的 xml 得到的,只是更改了我所说的(加上更改了我没有的字符串和颜色)
  • 是的,这就是它在布局预览中的显示方式。如果我真的运行代码,它不会显示出来。此外,如果我在 ListView 预览中预览它,它不会显示出来。令人沮丧...
  • @jacobduron - 你在代码中做了什么来改变视图吗?
  • 只是填充 TextViews。
【解决方案3】:

改变 相对布局 到 垂直布局

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多