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


当前回答

这是你的答案,这是一个在控件之间画线的例子。

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

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

这段代码在两个控件之间画线…

其他回答

使用此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" />

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

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

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

这将帮助您解决这个问题。 这里创建了一个小视图,用一条黑线作为两个视图之间的分隔符。

 <View
        android:layout_width="3dp"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
         />

例如,如果您使用recyclerView为您的项目:

在构建。gradle写:

dependencies {
    compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'

如果你想设置颜色,大小和边距值,你可以指定如下:

RecyclerView recyclerView = (RecyclerView) 
findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.RED)
                .sizeResId(R.dimen.divider)
                .marginResId(R.dimen.leftmargin, R.dimen.rightmargin)
                .build());

要完成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" />