2012-11-06 47 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 。
通過編程實現,您可以動態更改漸變