1

我有以下可绘制的 XML:

background_view_rounded_top.xml

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

    <selector>
        <item android:state_pressed="true">
            <shape>
                <gradient
                    android:angle="270.0"
                    android:endColor="@color/base_end_color_pressed"
                    android:startColor="@color/base_start_color_pressed" />

                <corners
                    android:bottomLeftRadius="0.0dip"
                    android:bottomRightRadius="0.0dip"
                    android:radius="2.0dip"
                    android:topLeftRadius="10.0dip"
                    android:topRightRadius="10.0dip" />
            </shape>
        </item>
        <item>
            <shape>
                <gradient
                    android:angle="270.0"
                    android:endColor="@color/base_end_color_default"
                    android:startColor="@color/base_start_color_default" />

                <corners
                    android:bottomLeftRadius="0.0dip"
                    android:bottomRightRadius="0.0dip"
                    android:radius="2.0dip"
                    android:topLeftRadius="11.0dip"
                    android:topRightRadius="11.0dip" />
            </shape>
        </item>
    </selector>

</inset>

我想改变它的startColorand endColor。我不能通过简单地复制这个 xml 几次来做到这一点,因为我会多次使用这个 drawable 并且有几十个这样的 xml 不是一个选项。所以我想重用这个 xml 并在代码中更改这些颜色。

我也有 background_view_rounded_bottom/middle/single 以及没有圆角的版本,所有这些 xmls 也应该有参数化的颜色。

4

2 回答 2

0

看看这个,有很多额外的代码,但它似乎演示了如何在代码中创建可绘制和渐变绘制...查看第 159 行。
通过以编程方式进行,您可以动态更改渐变

于 2012-11-06T15:47:03.503 回答
-4

尝试使用我创建的渐变。在这里您可以轻松设置开始和结束颜色,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#A5A5A5"
          android:endColor="#FFFFFF"
        android:angle="90" />
    <solid android:color="#50FFFFFF" />
    <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" 
     android:topLeftRadius="5dp" android:topRightRadius="5dp"/>
</shape>
于 2012-11-06T12:52:10.607 回答