Android在视图之间绘制一条水平线

Str*_*ing 98 android listview android-intent android-layout

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
        android:id="@+id/textView1"
        style="@style/behindMenuItemLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Twitter Feeds"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/list"
        android:layout_width="350dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/textView1"
        style="@style/behindMenuItemLabel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:text="FaceBook Feeds" />

    <ListView
        android:id="@+id/list1"
        android:layout_width="350dp"
        android:layout_height="50dp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的要求是在和之间绘制一条水平线TextViewListView

有人可以帮忙吗?

Bho*_*att 261

它将在TextView&之间绘制银灰色线条ListView

<TextView
    android:id="@+id/textView1"
    style="@style/behindMenuItemLabel1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:text="FaceBook Feeds" />

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#c0c0c0"/>

<ListView
    android:id="@+id/list1"
    android:layout_width="350dp"
    android:layout_height="50dp" />
Run Code Online (Sandbox Code Playgroud)

  • 这种硬编码颜色就像在网页中使用内联css一样.为什么这个投票如此之多?它应该使用android:background ="?android:attr/dividerHorizo​​ntal". (9认同)
  • 因为很容易实现 (4认同)
  • @Roel 这很简单,容易,而且有效。引用颜色而不是对其进行硬编码非常简单。我尝试了你的建议,我认为这在技术上会更好,但它似乎不起作用 - 我的布局中没有任何分隔线。我应该如何使用它?谢谢。 (3认同)
  • @Roel 使用它有什么缺点? (2认同)

vov*_*ost 21

您应该使用新的轻量级View Space来绘制分隔线.如果您将使用Space而不是,您的布局将加载更快View.

水平分隔线:

<android.support.v4.widget.Space
        android:layout_height="1dp"
        android:layout_width="match_parent" /> 
Run Code Online (Sandbox Code Playgroud)

垂直分隔线:

<android.support.v4.widget.Space
        android:layout_height="match_parent"
        android:layout_width="1dp" />
Run Code Online (Sandbox Code Playgroud)

您还可以添加背景:

<android.support.v4.widget.Space
        android:layout_height="match_parent"
        android:layout_width="1dp"
        android:background="?android:attr/listDivider"/>
Run Code Online (Sandbox Code Playgroud)

用法示例:

....
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="One"/>

<android.support.v4.widget.Space
    android:layout_height="match_parent"
    android:layout_width="1dp"
    android:background="?android:attr/listDivider"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Two"/>

<android.support.v4.widget.Space
    android:layout_height="match_parent"
    android:layout_width="1dp"
    android:background="?android:attr/listDivider"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Three"/>
....
Run Code Online (Sandbox Code Playgroud)

要使用,Space您应该在build.gradle中添加依赖:

dependencies {
    compile 'com.android.support:support-v4:22.1.+'
}
Run Code Online (Sandbox Code Playgroud)

文档https://developer.android.com/reference/android/support/v4/widget/Space.html

  • "太空"的使用将会令人失望,因为"太空"不会吸引任何东西(甚至不是背景).唯一的工作就是占用屏幕上的空间.解决方案是使用vanilla`View`元素.然后将根据需要绘制背景.(如果没有其他需要,您也可以避免支持库依赖.) (10认同)
  • `Space`继承自`View`,因此它继承了`View`定义的所有属性.(类似地,`LinearLayout`继承了`textAlignment`属性,即使它本身不显示任何文本.)随意自己测试(或者只看源代码),你会发现`Space`忽略所有与绘图相关的属性. (6认同)

zoz*_*lfo 20

在您要分隔的视图之间的布局中添加这样的内容:

  <View
       android:id="@+id/SplitLine_hor1"
       android:layout_width="match_parent"
       android:layout_height= "2dp"
       android:background="@color/gray" />
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你 :)

  • 它将绘制一条垂直线,因为它的高度为match_parent,宽度仅为2 dp. (2认同)

one*_*exf 12

创建一次并在需要的地方使用它是一个好主意.在styles.xml中添加:

<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/listDivider</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在需要行分隔符的xml代码中添加:

<View style="@style/Divider"/>
Run Code Online (Sandbox Code Playgroud)

最初由toddles_fp回答这个问题:布局中的Android绘图分隔符/分隔线


Xam*_*Xam 10

多年后,终于有了一个合适的视图来在 XML 布局上显示分隔符/分隔符/水平线。为 Android 开发 Material Components 的团队已经发布了版本 1.5.0-alpha01,其中添加了该类MaterialDivider。如前所述,这允许向布局添加分隔线。

首先,在 build.gradle 中添加以下内容:

implementation 'com.google.android.material:material:1.5.0-alpha01'

然后,在 XML 布局上您可以编写:

<com.google.android.material.divider.MaterialDivider
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

瞧,你有一个分隔器。如果你想自定义分隔线的颜色,有一个xml属性:app:dividerColor。此视图还允许通过样式设置主题。欲了解更多信息,您可以查看此链接


Xar*_*han 8

试试这个

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="?android:attr/listDivider"/>
Run Code Online (Sandbox Code Playgroud)


Jit*_*iya 5

<View
       android:id="@+id/view"
       android:layout_width="match_parent"
       android:layout_height="2dp"
       android:background="#000000" />
Run Code Online (Sandbox Code Playgroud)