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

时间:2013-10-01 11:45:44

标签: android android-layout listview android-intent

我的布局如下:

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

我的要求是在TextViewListView

之间绘制水平

有人可以帮忙吗?

17 个答案:

答案 0 :(得分:233)

它将在TextView&amp;之间绘制银灰色线条。 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="fill_parent"
    android:layout_height="2dp"
    android:background="#c0c0c0"/>

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

答案 1 :(得分:20)

您应该使用新的轻量级视图Update-Package –reinstall 来绘制分隔线。 如果您使用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"/> .... ,您应该在 build.gradle 中添加依赖项:

Space

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

答案 2 :(得分:16)

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

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

希望有所帮助:)

答案 3 :(得分:10)

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

答案 4 :(得分:7)

试试这个

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

答案 5 :(得分:3)

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

答案 6 :(得分:2)

多年后,终于有了在 XML 布局上显示分隔符/分隔符/水平线的正确视图。为 Android 开发 Material 组件的团队发布了 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。此视图还允许通过样式进行主题化。有关详细信息,您可以查看此 link

答案 7 :(得分:2)

您可以在视图之间添加此视图以模仿行

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

答案 8 :(得分:1)

试试这个

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

答案 9 :(得分:1)

创建水平线

如果您使用的是TextView,然后要放置一行,则使用View 这样,您可以使用任何提及蓝色,红色或黑色的背景色。

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

enter image description here

答案 10 :(得分:1)

试试这个适合我

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

答案 11 :(得分:1)

在您希望组件之间有分隔符的每个父LinearLayout中,添加android:divider="?android:dividerHorizontal"android:divider="?android:dividerVertical

根据LinearLayout的方向选择适当的。

直到我知道,此资源样式是从Android 4.3添加的。

答案 12 :(得分:0)

----&GT;简单的一个

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

答案 13 :(得分:0)

如果您不想仅为下划线使用额外视图。在textView上添加此样式。

style="?android:listSeparatorTextViewStyle"

就在这里,它会添加额外的属性,如

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

您可以轻松覆盖。

答案 14 :(得分:0)

对我有用的是

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

答案 15 :(得分: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"/>

app:layout_constraintStart_toStartOf="parent"/>

答案 16 :(得分:0)

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

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

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

相关问题