Android 在视图之间绘制水平线

问题描述 投票:0回答:19

我的布局如下:

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

我的要求是在TextView

ListView
之间画一条
水平

线

有人可以帮忙吗?

android android-layout listview android-intent
19个回答
329
投票

它将在

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

25
投票

您应该使用新的轻量级视图

Space
来绘制分隔线。 如果您使用
Space
而不是
View
,您的布局加载速度会更快。

水平分隔线:

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

垂直分隔线:

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

您还可以添加背景:

<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="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"/>
....

为了使用

Space
,您应该在build.gradle中添加依赖项:

dependencies {
    compile 'com.android.support:support-v4:22.1.+'
}

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


23
投票

在布局中要分离的视图之间添加类似的内容:

  <View
       android:id="@+id/SplitLine_hor1"
       android:layout_width="match_parent"
       android:layout_height= "2dp"
       android:background="@color/gray" />

希望有帮助:)


20
投票

创建一次并在需要的地方使用它是一个好主意。 将其添加到您的 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>

并将其添加到需要行分隔符的 xml 代码中:

<View style="@style/Divider"/>

最初由 toddles_fp 回答这个问题:Android Drawing Separator/Divider Line in Layout?


12
投票

试试这个

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="?android:attr/listDivider"/>

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

瞧,你有一个分隔器。如果你想自定义分隔线的颜色,有一个xml属性:

app:dividerColor
。此视图还允许通过样式设置主题。欲了解更多信息,您可以查看此链接


6
投票
<View
       android:id="@+id/view"
       android:layout_width="match_parent"
       android:layout_height="2dp"
       android:background="#000000" />

3
投票

Bhoomika 接受的答案进行了改进,以获得更精美的外观:

<View
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:background="@android:color/black"
     android:alpha="0.1"/>

android:alpha
用于设置视图的不透明度。


2
投票

您可以将此视图放在您的视图之间来模仿线条

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

2
投票

试试这个对我有用

 <View android:layout_width="1dp"
       android:layout_height="match_parent"
       android:background="@color/tw_composer" />

2
投票

创建水平线

如果您正在使用 TextView,然后您想放置一行,则使用 View 这样你就可以使用任何颜色,如蓝色、红色或黑色提及背景颜色。

 <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/black"></view>


1
投票

在您希望组件之间有分隔线的每个父级

LinearLayout
中,添加
android:divider="?android:dividerHorizontal"
android:divider="?android:dividerVertical

根据您的

LinearLayout
的方向在它们之间选择合适的。

据我所知,这个资源样式是从Android 4.3开始添加的。


1
投票

试试这个

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

1
投票

---->简单的一个

 <TextView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#c0c0c0"
    android:id="@+id/your_id"
    android:layout_marginTop="160dp" />

0
投票

可以指定背景颜色的视图(高度=几个 dpi)。 查看真实代码,它是:

        <LinearLayout
            android:id="@+id/lineA"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#000000" />

请注意,它可以是任何类型的

View


0
投票

如果您不想仅使用额外的视图来显示下划线。在您的

textView
上添加此样式。

style="?android:listSeparatorTextViewStyle"

缺点是它会添加额外的属性,例如

android:textStyle="bold"
android:textAllCaps="true"

您可以轻松覆盖它。


0
投票

对我有用的是

    <view
    android:layout_below="@+id/kaimla_number"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@color/golden"></view>

0
投票

 <view
        android:id="@+id/blackLine"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

应用程序:layout_constraintStart_toStartOf =“父”/>


0
投票

使用此视图标签,您可以水平或垂直地创建视图,只需更改高度和宽度即可使其水平和垂直。您可以简单地创建水平和垂直线。 这是水平线代码,其中视图的高度是线的厚度。

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="1.5dp"
            android:layout_marginHorizontal="24dp"
            android:layout_marginTop="20dp"
            android:background="@color/green"

            app:layout_constraintTop_toBottomOf="@id/imgChoice" />
      
© www.soinside.com 2019 - 2024. All rights reserved.