98

我正在尝试将分隔线添加到水平线性布局,但无处可去。分隔线只是没有显示。我是 Android 的新手。

这是我的布局 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/llTopBar"
        android:orientation="horizontal"
        android:divider="#00ff00"
        android:dividerPadding="22dip"
        android:showDividers="middle">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf" />
            
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf" />
    
    </LinearLayout>
    
</RelativeLayout>
4

11 回答 11

231

将此用于水平分隔线

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

这对于垂直分隔线

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

或者,如果您可以使用 LinearLayout 分隔线,用于水平分隔线

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:height="1dp"/>
    <solid android:color="#f6f6f6"/>
</shape>

在线性布局中

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

如果您想使用垂直分隔线,则代替android:height="1dp"形状使用android:width="1dp"

提示:不要忘记android:showDividers项目。

于 2013-02-28T06:17:09.770 回答
69

试试这个,在res/drawable文件夹中创建一个分隔符:

vertical_divider_1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <size android:width="1dip" />
    <solid android:color="#666666" />    
</shape> 

divider像这样使用 LinearLayout 中的属性:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="@drawable/vertical_divider_1"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ffffff" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

注意: android:divider仅适用于 Android 3.0(API 级别 11)或更高版本。

于 2013-02-28T06:24:31.967 回答
41

在布局中添加分隔线很容易,我们不需要单独的视图。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:divider="?android:listDivider"
    android:dividerPadding="2.5dp"
    android:orientation="horizontal"
    android:showDividers="middle"
    android:weightSum="2" ></LinearLayout>

上面的代码为LinearLayout

于 2014-10-21T06:51:33.717 回答
17

更新:使用 AppCompat 的 pre-Honeycomb

如果您使用的是 AppCompat 库 v7,您可能需要使用该LinearLayoutCompat视图。使用这种方法,您可以在 Android 2.1、2.2 和 2.3 上使用可绘制分隔线。

示例代码:

<android.support.v7.widget.LinearLayoutCompat
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:showDividers="middle"
        app:divider="@drawable/divider">

drawable/divider.xml:(顶部和底部有一些填充的分隔线)

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetBottom="2dp"
        android:insetTop="2dp">
    <shape>
        <size android:width="1dp" />
        <solid android:color="#FFCCCCCC" />
    </shape>
</inset>

非常重要的注意事项:视图LinearLayoutCompat不会扩展LinearLayout,因此您不应使用android:showDividersorandroid:divider属性,而应使用自定义属性:app:showDividersand app:divider。在代码中,您还应该使用LinearLayoutCompat.LayoutParamsnot LinearLayout.LayoutParams

于 2015-03-13T16:08:36.000 回答
8

我今天遇到了同样的问题。正如前面的答案所示,问题源于在分隔标签中使用颜色,而不是可绘制对象。但是,与其编写自己的可绘制 xml,我更喜欢尽可能多地使用主题属性。您可以使用 android:attr/dividerHorizo​​ntal 和 android:attr/dividerVertical 来获取预定义的可绘制对象:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle"
    android:divider="?android:attr/dividerVertical"
    android:orientation="horizontal">
    <!-- other views -->
</LinearLayout>

这些属性在 API 11 及更高版本中可用。

此外,正如 bocekm 在他的回答中所提到的,dividerPadding 属性不会像人们假设的那样在垂直分隔线的任一侧添加额外的填充。相反,它定义了顶部和底部填充,因此如果它太大,可能会截断分隔线。

于 2015-03-31T16:55:13.483 回答
7

您可以使用内置的分隔器,这将适用于两个方向。

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="?android:attr/listDivider"
  android:orientation="horizontal"
  android:showDividers="middle">
于 2016-02-29T16:53:21.617 回答
3

令人沮丧的是,您必须在活动中启用显示代码中的分隔符。例如:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the view to your layout
    setContentView(R.layout.yourlayout);

    // Find the LinearLayout within and enable the divider
    ((LinearLayout)v.findViewById(R.id.llTopBar)).
        setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);

}
于 2013-06-28T22:04:32.413 回答
3

如果Kapil Vats的答案不起作用,请尝试以下操作:

可绘制/divider_horizo​​ntal_green_22.xml

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

    <size android:width="22dip"/>
    <solid android:color="#00ff00"/>

</shape>

布局/your_layout.xml

LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/llTopBar"
            android:orientation="horizontal"
            android:divider="@drawable/divider_horizontal_green_22"
            android:showDividers="middle"
           >

我遇到了 padding 属性不起作用的问题,因此我必须直接在分隔符中设置分隔符的高度。

笔记:

如果你想在垂直LinearLayout中使用它,就新建一个,像这样: drawable/divider_vertical_green_22.xml

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

    <size android:height="22dip"/>
    <solid android:color="#00ff00"/>

</shape>
于 2016-04-08T11:28:07.753 回答
2

由于 dividerPadding 太大,您的分隔线可能不会显示。您设置了 22dip,这意味着分隔线从顶部截断 22dip,从底部截断 22dip。如果您的布局高度小于或等于 44dip,则没有分隔线可见。

于 2014-05-15T16:09:29.660 回答
0

为了被绘制,分隔线LinearLayout必须有一些高度,而ColorDrawable(本质上#00ff00与任何其他硬编码颜色一样)没有。解决此问题的简单(正确)方法是将颜色包装成Drawable具有预定义高度的颜色,例如shape可绘制

于 2014-01-06T17:47:31.437 回答
-1

您必须为诸如 textview 或 imageview 之类的分隔符创建任何视图,然后为其设置背景,如果您有图像,则使用颜色作为背景。

希望这对您有所帮助。

于 2013-02-28T06:20:25.293 回答