如何将(垂直)分隔符添加到水平LinearLayout?

Ahm*_*nas 85 android divider android-layout android-linearlayout

我正在尝试将分隔符添加到水平线性布局但是无处可去.分频器没有显示.我是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>
Run Code Online (Sandbox Code Playgroud)

Kap*_*ats 205

用它作水平分隔线

<View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/honeycombish_blue" />
Run Code Online (Sandbox Code Playgroud)

这适用于垂直分隔线

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/honeycombish_blue" />
Run Code Online (Sandbox Code Playgroud)

或者,如果可以使用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>
Run Code Online (Sandbox Code Playgroud)

在LinearLayout中

<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" >
Run Code Online (Sandbox Code Playgroud)

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

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

  • 谢谢,但为什么它在这里:s http://developer.android.com/reference/android/widget/LinearLayout.html (9认同)
  • 看起来你的`layout_width`和`layout_height`值混淆了:对于水平`layout_width`应该是``fill_parent'`和`layout_height`应该是``1dp"`.对于垂直分频器,应该进行类似的交换. (7认同)
  • 谢谢.但是我如何将它添加到"android:divider"属性中呢?基本上,我的意思是,在每个元素之间添加分隔符的某种自动方式?我的意思是,为什么android:divider属性存在? (3认同)

Shr*_*a S 65

试试这个,在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> 
Run Code Online (Sandbox Code Playgroud)

并使用dividerLinearLayout中的属性,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

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


kha*_*ntt 37

将分隔符添加到布局很容易,我们不需要单独的视图.

<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>
Run Code Online (Sandbox Code Playgroud)

上面的代码为垂直分隔符 LinearLayout


Rol*_*f ツ 16

更新:使用AppCompat预蜂窝

如果您使用的是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">
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

非常重要的注意事项:LinearLayoutCompat视图不会扩展LinearLayout和为此你不应该使用android:showDividersandroid:divider性质,但自定义的:app:showDividersapp:divider.在代码中你也应该使用而LinearLayoutCompat.LayoutParams不是LinearLayout.LayoutParams!


Nic*_*sen 8

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

<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>
Run Code Online (Sandbox Code Playgroud)

API 11及更高版本中提供了这些属性.

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


Ami*_*ade 6

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

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="?android:attr/listDivider"
  android:orientation="horizontal"
  android:showDividers="middle">
Run Code Online (Sandbox Code Playgroud)