【问题标题】:Android draw a Horizontal line between viewsAndroid在视图之间画一条水平线
【发布时间】:2013-10-07 13:30:01
【问题描述】:

我的布局如下:

<?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 之间画一条水平线

有人可以帮忙吗?

【问题讨论】:

  • 您可以在现有视图的背景中添加线条,而不是添加额外的线条。或者您可以将所有视图放在 listView 中。 listView 可以绘制分隔符。最简单的方法是添加额外的视图。

标签: android android-layout listview android-intent


【解决方案1】:

它将在TextViewListView之间绘制银灰色线

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

【讨论】:

  • 这种硬编码的颜色就像在网页中使用内联 css。为什么这个投票这么多?例如,它应该使用 android:background="?android:attr/dividerHorizo​​ntal"。
  • 因为容易实现
  • @Roel 它简单、容易而且有效。引用颜色而不是硬编码它很简单。我尝试了您的建议,我认为这在技术上会更好,但它似乎不起作用 - 我的布局中没有任何分隔线。我应该如何使用它?谢谢。
  • @Roel 使用它有什么缺点?
  • 至少在单独的文件中设置颜色并使用颜色变量。不利的一面是,如果您想更改应用的颜色/样式,您可能必须更改许多文件。
【解决方案2】:

您应该使用新的轻量级视图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

【讨论】:

  • Space 的使用会令人失望,因为Space 什么都不画(甚至不画背景)。它的唯一工作是占用屏幕上的空间。解决方案是改用香草View 元素。然后将根据需要绘制背景。 (如果不需要,您也可以避免支持库依赖。)
  • Space 继承自 View,因此它继承了 View 定义的所有属性。 (类似地,LinearLayout 继承了 textAlignment 属性,即使它本身不显示任何文本。)您可以自己测试一下(或者只查看源代码),您会发现 Space 忽略所有绘图相关的属性。
  • 是的,Space 不绘制背景,它是透明的。
【解决方案3】:

在你想要分离的视图之间的布局中添加这样的内容:

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

希望对你有帮助:)

【讨论】:

  • 它将绘制一条垂直线,因为它的高度是 match_parent 而宽度只有 2 dp。
【解决方案4】:

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

【讨论】:

  • 哇。这是最好的。希望您还可以添加示例来演示使用垂直和水平分隔线以确保完整性。
【解决方案5】:

试试这个

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

【讨论】:

    【解决方案6】:
    <View
           android:id="@+id/view"
           android:layout_width="match_parent"
           android:layout_height="2dp"
           android:background="#000000" />
    

    【讨论】:

    • 我们不能以编程方式访问这个元素吗?
    【解决方案7】:

    多年后,终于有了在 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。此视图还允许通过样式进行主题化。欲了解更多信息,您可以查看此link

    【讨论】:

      【解决方案8】:

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

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

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

      【讨论】:

        【解决方案9】:

        你可以把这个视图放在你的视图之间模仿线

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

        【讨论】:

          【解决方案10】:

          试试这个对我有用

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

          【讨论】:

            【解决方案11】:

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

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

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

            【讨论】:

            • 我现在几乎只使用约束布局,但这可能会在某些时候派上用场。
            【解决方案12】:

            试试这个

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

            【讨论】:

              【解决方案13】:

              创建水平线

              如果您正在使用 TextView,然后您想放置一个 Line 然后使用 View 通过这种方式,您可以使用任何颜色,如蓝色、红色或黑色提及背景颜色。

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

              【讨论】:

                【解决方案14】:

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

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

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

                【讨论】:

                  【解决方案15】:

                  ---->简单的一个

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

                  【讨论】:

                  • 不错的一个。 View 方法是否更通用?
                  【解决方案16】:

                  如果您不想为下划线使用额外的视图。将此样式添加到您的 textView

                  style="?android:listSeparatorTextViewStyle"
                  

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

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

                  您可以轻松覆盖。

                  【讨论】:

                    【解决方案17】:

                    适合我的是

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

                    【讨论】:

                      【解决方案18】:

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

                      【讨论】:

                      • 您不应该只添加屏幕截图而不是 StackOverflow 或任何其他在线论坛上的代码。这让任何人尝试测试或实现您的代码变得更加困难。
                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2012-05-20
                      • 1970-01-01
                      相关资源
                      最近更新 更多