2012-11-06 48 views
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> 

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

我也有background_view_rounded_round_bottom /中/单和也没有圆角的版本,所有这些xmls也应该有参数化的颜色。

回答

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

只需用此替换您的xml。你还需要什么。 –

+1

你不明白我的问题。你仍然需要几十个xmls来改变颜色。 – SuitUp

+0

您需要更改哪种颜色以及用于何种目的。我无法得到你所说的。 –

0

检查this出来,有相当多的额外代码,但它似乎展示了如何在代码中创建一个可绘制和梯度绘制......环顾四周线159 。
通过编程实现,您可以动态更改渐变