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


当前回答

我通常使用这样的代码:

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:background="#aa000000" />

如果你在你的布局中有一个对象,你想在下面设置line,使用ImageView中的这个属性:

android:layout_below="@+id/textBox1"

其他回答

我通常使用这段代码添加水平线:

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

若要添加垂直分隔符,请切换layout_width和layout_height值

为了改进Alex Kucherenko和Dan Dar3提供的答案

我把这个添加到我的样式中:

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

然后在我的布局是更少的代码和更容易阅读。

<View style="@style/Divider"/>
//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

如果你要经常使用它,最好的办法是

styles.xml:

<style name="Seperator">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item>
        <item name="android:background">@color/light_color</item>
    </style>

现在在你的布局中,像这样添加它:

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

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

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