在LinearLayout中更改分割线的颜色

12

我可以知道如何改变LinearLayout中分隔符的颜色吗?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="?android:attr/dividerVertical"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ff2d2d2d" >
...
</LinearLayout>

我需要手动将Android SDK中的9 patch图像复制到我的项目中,并定义自己的属性来引用它吗?


是的,您可以创建自己的图像(dividerVertical),并将其放入drawable中,然后使用android:divider="@drawable/dividerVertical"来使用它。我已经在我的端上尝试过了,它可以正常工作。 - Akbari Dipali
3个回答

27
似乎android:divider属性不接受颜色值。因此,您需要创建一个单独的分隔符drawable才能使其正常工作:

divider.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="#f00" />

</shape>

layout.xml

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

此外,请注意,android:divider 仅适用于 Android 3.0 或更高版本,并且在早期版本的 Android 中无法使用。

你应该排除无效答案。 - lhunath
为什么我在使用这个XML时看不到分隔线?我甚至尝试了不同的宽度,并在所有情况下显示分隔线... - android developer
我认为分隔符属性不再起作用了。有一个视图解决方案,它不依赖于这个属性。 - The Original Android

-2

这是我做的方式

      <ImageView
            android:id="@+id/imgVwmarkupborder"
            android:layout_width="280dp"
            android:layout_height="2dp"
            android:src="@android:color/white" />

-2
<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/white"/>

在我的方法中,我使用了这个。


我知道这种方法。我只是希望使用android:divider功能。 - Cheok Yan Cheng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接