【问题标题】:how can I create a division between items in a listview如何在列表视图中的项目之间创建划分
【发布时间】:2017-06-24 23:57:12
【问题描述】:

我正在开发包含 4 或 5 个项目的列表视图的 android 应用程序。类似于 gmail 应用程序如何显示电子邮件 http://pctechtips.org/data/unnamed.png 但我很难用它们之间的一些可见线来分离这些项目。比如边框什么的。这样做的正确方法是什么?还有什么是垂直居中文本的正确方法? 谢谢

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.example.george.droidnet.MainActivity">

    <TextView
        android:id="@+id/hostname"
        android:layout_height="100dp"
        android:layout_width="match_parent"
        android:background="@color/primary"
        android:textSize="24sp"
        />

    <TextView
        android:id="@+id/address"
        android:layout_height="100dp"
        android:layout_width="match_parent"
        android:text="IP Address"
        android:background="@color/primary_light"
        android:gravity="top|bottom"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/connections"
        android:text="Connections"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/primary_light"
        />

    <TextView
        android:id="@+id/DNS Queries"
        android:background="@color/primary_light"
        android:text="Ping"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/Ping Host"
        android:background="@color/primary_light"
        android:text="Ping"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

【问题讨论】:

  • 请搜索如何给ListViewLinearLayout添加分隔线
  • 有很多方法,例如在列表视图中添加分隔线或在底部列表视图项中添加填充/边距

标签: android xml listview android-studio interface


【解决方案1】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.george.droidnet.MainActivity">

<TextView
    android:id="@+id/hostname"
    android:layout_height="100dp"
    android:layout_width="match_parent"
    android:background="@color/primary"
    android:textSize="24sp"
    />

<TextView
    android:id="@+id/address"
    android:layout_height="100dp"
    android:layout_width="match_parent"
    android:text="IP Address"
    android:background="@color/primary_light"
    android:gravity="top|bottom"
    android:textSize="24sp" />

<TextView
    android:id="@+id/connections"
    android:text="Connections"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/primary_light"
    />

<TextView
    android:id="@+id/DNS Queries"
    android:background="@color/primary_light"
    android:text="Ping"
    android:layout_width="match_parent"
    android:layout_height="100dp" />

<TextView
    android:id="@+id/Ping Host"
    android:background="@color/primary_light"
    android:text="Ping"
    android:layout_width="match_parent"
    android:layout_height="100dp" />
<View
    android:layout_width="match_parent"
    android:layout_height="2dp" <!--  Set width of the line--> />
</LinearLayout>

【讨论】: