2009-10-25 73 views
48

我有一个渐变的形状,我用作ListView项目之间的分隔线。我已经定义如下它:如何在Android中添加填充到梯度<shape>?

<?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",它似乎都不起作用。我可以把它放在<shape>元素,<gradient>元素,或者<shape>的一个单独的<padding>子元素中,它不会改变任何东西。

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

+0

我已经更新了所有的答案使用DP,因为大部分的时间开发商不应该PX使用dp考虑到不同的屏幕密度:http://developer.android.com/guide/practices/screens_support.html – Intrications 2013-02-10 11:51:50

+2

除了评论和解决方案说不使用px,使用dp - 我想补充一点渐变没有高度属性,在这种情况下无用。高度应作为元素的属性应用。 – prodaea 2013-03-14 14:46:40

+0

可能重复[填充不会影响在XML布局](http://stackoverflow.com/questions/1283085/padding-doesnt-affect-shape-in​​-an-xml-layout) – tir38 2016-07-22 17:27:57

回答

20

一个解决方案似乎是用另一个指定适当填充的drawable“包装”我的drawable。

例如,list_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> 

然后list_divider_inner.xml将是原来的绘制:

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

这将导致两个文件虽然指定一个简单的分频器。我不知道是否有办法只用一个文件来完成。

+1

px将看*很棒*现代xhdpi手机。 - 不要使用px ** ever **。使用** dp **代替图形,使用** sp **代替字体大小。阅读:http://developer.android.com/guide/practices/screens_support.html – Martin 2012-05-02 13:15:21

+1

@Martin我已经更新了使用dp的答案,以避免复制和粘贴不良做法。 – Intrications 2013-02-10 11:49:49

+6

这个问题是关于在列表视图项之间使用精细分隔符的。对于这种特殊情况,我确实想要使用单个像素分频器,而不管设备的分辨率如何,所以px是正确的。 – emmby 2013-02-11 01:52:21

131

我想你可以像这样结合起来:

<?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> 
+1

该解决方案更好,因为它只使用一个文件。 – 2011-06-20 09:49:39

+1

作品奇妙的有一丝变化: android:right =“6px”/> 应该是 android:right =“6px”> – OrhanC1 2012-03-15 16:52:27

+9

当然,一个不应该使用px - 总是使用** dp **或** sp **。 – Martin 2012-05-02 13:08:53

45

另一种解决方案采用插入式:

<?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> 
+0

@ntrications:无需编辑,我使用px代替dp,因为提问者需要。 – Wayne 2013-02-19 02:07:51

+1

这样,你也可以在选择器文件中使用它。 – elimirks 2013-09-10 16:39:38