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


当前回答

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

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

其他回答

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item
    android:bottom="0dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="-2dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="1dp"
            android:color="@color/divider" />
    </shape>
</item>

我通常使用这样的代码:

<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="#000"/>

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

如果你使用actionBarSherlock,你可以使用com.actionbarsherlock.internal.widget.IcsLinearLayout类来支持分隔符并在视图之间显示它们。

用法示例:

<com.actionbarsherlock.internal.widget.IcsLinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:divider="@drawable/divider"
    android:dividerPadding="10dp"
    android:orientation="vertical"
    android:showDividers="beginning|middle|end" >
... children...

res /可拉的/ divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <size android:height="2dip" />

    <solid android:color="#FFff0000" />

</shape>

请注意,由于某些原因,图形设计器中的预览显示为“android.graphics.bitmap_delegate.nativeRecycle(I)Z”。不确定这意味着什么,但它可以被忽略,因为它在新版本的android和旧版本上都很好(在android 4.2和2.3上测试)。

似乎只有在图形设计器使用API17时才会显示错误。

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

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

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