Android 如何将(垂直)分隔线添加到水平线性布局?

Android 如何将(垂直)分隔线添加到水平线性布局?,android,android-layout,android-linearlayout,divider,Android,Android Layout,Android Linearlayout,Divider,我试图在水平线性布局中添加一个分隔符,但却一无所获。分隔符就是不显示。我对安卓完全是个新手 这是我的布局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_heigh

我试图在水平线性布局中添加一个分隔符,但却一无所获。分隔符就是不显示。我对安卓完全是个新手

这是我的布局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>

用于水平分隔器

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

这是垂直分隔线

<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" />
<?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分隔符,则可以将其用作水平分隔符

<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" />
<?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
项。

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


希望这对你有帮助

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

垂直切割器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> 

注意:
android:divider
仅在android 3.0(API级别11)或更高版本中可用

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

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

}

为了绘制,
LinearLayout
的分隔线必须有一定的高度,而
可着色的
(基本上是
#00ff00
以及任何其他硬编码的颜色)没有。解决这一问题的简单(正确)方法是将您的颜色包装成一些具有预定义高度的
可绘制
,例如
形状
可绘制

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

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

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

以上代码为
线性布局制作垂直分隔器

更新:使用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">

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



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

我今天遇到了同样的问题。如前面的答案所示,问题源于在分隔符标记中使用颜色,而不是可绘制的颜色。然而,我宁愿尽可能多地使用主题属性,而不是编写自己的可绘制xml。您可以使用android:attr/dividerHorizontal和android:attr/dividervertial来获得预定义的可绘制图形:

<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属性不会像人们可能假设的那样在垂直分隔符的两侧添加额外的填充。相反,它定义了顶部和底部填充,因此如果分隔符太大,可能会截断分隔符。

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

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="?android:attr/listDivider"
  android:orientation="horizontal"
  android:showDividers="middle">

如果的答案无效,请尝试以下方法:

可绘图/分隔器\u水平\u绿色\u 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>
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"
           >
<?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>
我遇到了一个padding属性不起作用的问题,因此我必须直接在分隔符中设置分隔符的高度

注意:

<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">
如果要在垂直线性布局中使用它,请创建一个新布局,如下所示: 可绘图/分隔器\u垂直\u绿色\u 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>
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"
           >
<?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>


您在哪个版本的Android上运行此功能?setDividerDrawable仅在API 11jelly bean 4.2 API 17之后存在。如果您尝试了其他方法,请确保LinearLayout的方向正确。为水平方向的分隔器设置高度将非常混乱。不要忘记令人恼火的SHOWDIVIDERS项目!!!!!!但这只会增加一个分隔符。。假设我有10个元素,为每个元素之间的分隔符添加额外的代码就像一个waste@death_relic0为什么不为分隔符创建一个单独的布局,然后使用include标记将其添加到任意位置和任意时间。我想这会让我更好,不会浪费。谢谢。但是我如何将其添加到“android:divider”属性中呢?基本上,我的意思是,某种自动的方式在每个元素之间添加分隔符?我的意思是,这不就是为什么会有android:divider属性吗?@death\u relic0 android:divider是ListView、Expandable ListView和TabWidgetHanks的avil,但为什么会在这里呢,似乎您的
layout\u width
layout\u height
值混淆了:对于水平
layout\u width
应该是
“fill\u parent”
layout\u height
应该是
“1dp”
。应该类似地替换为垂直分隔符。我总是忘记showDividers属性。非常感谢。感谢您提供使用提示?android:listDivider。我只是注意到这在API 21或更高版本中是不可见的。在较低的API版本中,显示一条小灰线shown@KetanMehta我们将使用属性“android:divider”来定义它,它是可绘制的或颜色的。android:divider是否支持API 15+?这是向分隔符添加垂直填充的唯一方法吗?@SARose否,您始终可以创建