如何添加填充到梯度LT;形状>在Android的? [英] How to add padding to gradient <shape> in Android?

查看:103
本文介绍了如何添加填充到梯度LT;形状>在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用的的ListView 项之间的分隔一个渐变的形状。我已经将它定义如下:

I have a shape with a gradient that I'm using as a divider between ListView items. I've defined it as follows:

<?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个像素上的梯度的任一侧,因此,它并没有从边缘延伸到屏幕边缘

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.

不过,无论身在何处,我把一个机器人:左=6px安卓权=6px,它似乎并没有生效。我可以把它放在&LT;形状&GT; 元素,在&LT;梯度GT; 元素,或者在一个单独的&LT;填充&GT; 的子&LT;形状&GT; ,它不会改变任何东西。

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.

我如何添加填充物在我的名单分左右?

How can I add padding on the left and right of my list divider?

推荐答案

一个解决方案似乎是包装我的绘制与另一个绘制指定适当的填充。

One solution seems to be to "wrap" my drawable with another drawable that specifies the appropriate padding.

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

这将导致两个文件虽然规定一个简单的除法。我不知道是否有一种方法,只有一个文件,但做到这一点。

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.

这篇关于如何添加填充到梯度LT;形状&GT;在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆