如何在 Android 中为渐变 <shape> 添加填充?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1622364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add padding to gradient <shape> in Android?
提问by emmby
I have a shape with a gradient that I'm using as a divider between ListView
items. I've defined it as follows:
我有一个渐变形状,用作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>
I would like to add 6 pixels of padding on either side of the gradient, so that it doesn't extend from edge to edge of the screen.
我想在渐变的两侧添加 6 个像素的填充,这样它就不会从屏幕的边缘延伸到边缘。
However, no matter where I put an android:left="6px"
and android:right="6px"
, it doesn't seem to take effect. I can put it in the <shape>
element, the <gradient>
element, or in a separate <padding>
child of <shape>
, and it doesn't change anything.
但是,无论我把android:left="6px"
and放在哪里android:right="6px"
,它似乎都不起作用。我可以把它放在<shape>
元素、<gradient>
元素中,或者放在 的一个单独的<padding>
子元素中,<shape>
它不会改变任何东西。
How can I add padding on the left and right of my list divider?
如何在列表分隔符的左侧和右侧添加填充?
采纳答案by emmby
One solution seems to be to "wrap" my drawable with another drawable that specifies the appropriate padding.
一种解决方案似乎是用另一个指定适当填充的可绘制对象“包装”我的可绘制对象。
For example, list_divider.xml
would be:
例如,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>
And then list_divider_inner.xml
would be the original drawable:
然后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>
This results in two files to specify a simple divider though. I don't know if there's a way to do it with only one file though.
这会导致两个文件指定一个简单的分隔符。我不知道是否有办法只用一个文件来做到这一点。
回答by anon
I guess you could combine it like this:
我想你可以像这样组合它:
<?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>
回答by Wayne
Another solution using inset:
使用插图的另一种解决方案:
<?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>