【问题标题】:How to add padding to gradient <shape> in Android?如何在 Android 中为渐变 <shape> 添加填充?
【发布时间】:2009-10-25 22:51:53
【问题描述】:

我有一个渐变形状,用作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"放在哪里,它似乎都没有生效。我可以把它放在&lt;shape&gt; 元素、&lt;gradient&gt; 元素中,或者放在&lt;shape&gt; 的一个单独的&lt;padding&gt; 子元素中,它不会改变任何东西。

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

【问题讨论】:

标签: android


【解决方案1】:

我猜你可以这样组合:

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

【讨论】:

  • 这个解决方案更好,因为它只使用一个文件。
  • 只要稍作改动就可以很好地工作:android:right="6px" /> 应该是 android:right="6px" >
  • 当然不应该使用 px — 总是使用 dpsp
  • @Martin 除非您使用 1px 的高度作为列表分隔符。见stackoverflow.com/questions/3979218/android-listview-divider
  • @Martin 会看看,你的 dp 定义的分隔符在低密度屏幕上看起来有多大。
【解决方案2】:

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

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

【讨论】:

  • @ntrications:无需编辑,我在这里使用 px 而不是 dp 因为提问者想要那个。
  • 这样,您也可以将它放在选择器文件中。
  • 完美。正是我需要的。
【解决方案3】:

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

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

这会导致两个文件指定一个简单的分隔符。我不知道是否有办法只使用一个文件。

【讨论】:

  • px 看起来 伟大 是一部现代 xhdpi 手机。永远不要使用 px。对图形使用 dp,对字体大小使用 sp。阅读:developer.android.com/guide/practices/screens_support.html
  • @Martin 我已经更新了使用 dp 来避免复制和粘贴不良做法的答案。
  • 这个问题是关于在列表视图项之间使用细分隔符。对于这种特殊情况,无论设备的分辨率如何,我都特别想使用单个像素分割器,所以 px 是正确的。
  • 这是我正在寻找的最佳解决方案!我只需要向本机分隔符添加填充,因此我将"@android:drawable/divider_horizontal_dark" 包装到层列表容器中。谢谢!
  • 谢谢 由于某种原因 inset 在 android 4.2.2 中不起作用。只有这个解决方案有效。
猜你喜欢
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多