如何将填充添加到渐变<;形状>;在Android中?

如何将填充添加到渐变<;形状>;在Android中?,android,Android,我有一个带有渐变的形状,我用它作为ListView项之间的分隔符。我将其定义如下: <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#ccd0d3" andro

我有一个带有渐变的形状,我用它作为
ListView
项之间的分隔符。我将其定义如下:

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

<gradient
    android:startColor="#ccd0d3"
    android:centerColor="#b6babd"
    android:endColor="#ccd0d3"
    android:height="1px"
    android:angle="0" />

</shape>

我想在渐变的两侧添加6个像素的填充,这样它就不会从屏幕的边缘延伸到另一个边缘

然而,无论我把一个
android:left=“6px”
android:right=“6px”
放在哪里,它似乎都没有生效。我可以把它放在
元素、
元素中,或者放在
的一个子元素中,它不会改变任何东西


如何在列表分隔符的左侧和右侧添加填充?

一种解决方案似乎是用另一个指定适当填充的可绘制表格“包装”我的可绘制表格

例如,
list\u divider.xml
将是:

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

    <item
        android:left="6dp"
        android:right="6dp"
        android:drawable="@drawable/list_divider_inner" />

</layer-list>

这将导致两个文件指定一个简单的分隔符。但我不知道是否有一种方法可以只使用一个文件。

我想你可以这样组合它:

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

    <item android:left="6dp"
          android:right="6dp">

        <shape android:shape="rectangle">
            <gradient android:startColor="#ccd0d3"
                      android:centerColor="#b6babd"
                      android:endColor="#ccd0d3"
                      android:height="1px"
                      android:angle="0"/>
        </shape>
    </item>
</layer-list>

使用插图的另一种解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="6dp"
    android:insetRight="6dp" >

    <shape   
        android:shape="rectangle">

    <gradient
        android:startColor="#ccd0d3"
        android:centerColor="#b6babd"
        android:endColor="#ccd0d3"
        android:height="1px"
        android:angle="0" />

    </shape>

</inset>


此解决方案更好,因为它只使用一个文件。只要稍作更改,效果就会非常好:安卓:right=“6px”/>应该是安卓:right=“6px”>当然不应该使用px-始终使用dp或sp.@Martin,除非列表分隔符的高度为1px。请看@Martin To look,您的dp定义的分隔符在低密度屏幕上看起来有多大。px将非常适合现代xhdpi手机。-永远不要使用px。改用dp表示图形,sp表示字体大小。阅读:@Martin我已经更新了答案,使用dp来避免复制和粘贴不良做法。这个问题是关于在列表视图项之间使用精细分隔符。对于这种特殊情况,我确实特别想使用单像素分割器,无论设备的分辨率如何,因此px是正确的。这是我正在寻找的最佳解决方案!我只需要在本机分隔符中添加填充,因此我将“@android:drawable/divider\u horizontal\u dark”包装到层列表容器中。塔克斯!由于某种原因,inset在android 4.2.2中不起作用。只有这个解决方案有效。我已经更新了所有答案,使用dp而不是px,因为大多数时候开发人员都应该使用dp来考虑不同的屏幕密度:除了说明和解决方案不使用px之外,使用dp-我想补充一点,渐变没有高度属性,在这种情况下是无用的。高度应该作为元素的属性应用。可能重复@ntrications:无需编辑,我在这里使用px而不是dp,因为提问者需要。这样,你也可以在选择器文件中使用它。完美。正是我需要的。
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="6dp"
    android:insetRight="6dp" >

    <shape   
        android:shape="rectangle">

    <gradient
        android:startColor="#ccd0d3"
        android:centerColor="#b6babd"
        android:endColor="#ccd0d3"
        android:height="1px"
        android:angle="0" />

    </shape>

</inset>