我想在布局的中间画一条线,并使用它作为其他项目的分隔符,如TextView。有没有一个好的小部件。我真的不想使用图像,因为它很难匹配其他组件到它。我也想让它有相对的位置。谢谢


当前回答

添加此视图;在你的textview之间画一个分隔符

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />

其他回答

将空间分成两等份:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="?android:dividerHorizontal"
        android:showDividers="end"></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></LinearLayout>

</LinearLayout>

注意,其中一部分在结尾包含分隔符

使用此XML代码添加垂直线

 <View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:background="#000000" />

使用此XML代码添加水平线

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />

要完成Camille Sévigny回答,您可以另外定义自己的线形状,例如自定义线的颜色。

在可绘制目录中定义一个xml形状。line_horizontal.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:shape="line">
    <stroke android:width="2dp" android:color="@android:color/holo_blue_dark" />
    <size android:width="5dp" />
</shape>

在你的布局中使用这一行和你想要的属性:

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="2dp"
        android:src="@drawable/line_horizontal" />
//for vertical line:

<View
   android:layout_width="1dp"
   android:layout_height="fill_parent"
   android:background="#00000000" />




//for horizontal line: 

<View
   android:layout_width="fill_parent"
   android:layout_height="1dp"
   android:background="#00000000" />
//it works like a charm

这很简单。只需要创建一个背景颜色为黑色的视图。

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000"/>

这将创建一个具有背景色的水平线。您还可以添加其他属性,如边距,填充等,就像任何其他视图。