2016-10-10 83 views
1

已經繪製用一種顏色來定義:如何重用與不同顏色的定義繪製在XML

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
<corners android:radius="10dip"/> 
<solid android:color="#FF7C71BF"/> 

,並在佈局用於一些項目如:

android:background="@drawable/oval_shape" 

如果我想要在多個地方使用不同顏色的佈局xml中的drawable,怎麼做? (可以在代碼中完成重新分配顏色,只是想看看是否可以使用xml來完成)。

回答

0

你不能用XML做它相信我。

在Java代碼中簡單地寫如下:

View v = findViewById(R.id.view_id); 
LayerDrawable bg = (LayerDrawable)v.getBackground(); 
final GradientDrawable shape = (GradientDrawable) bg.findDrawableByLayerId(R.id.drawable_id); 
shape.setColor([any color]); 
+0

感謝阿米爾!我們知道它可以不用代碼,只是想知道是否有辦法在XML中做到這一點。 – lannyf

2

這裏有一個解決方案,只用棒棒糖和後期的作品:

使顏色白:

<solid android:color="#FFFFFFFF"/> 

然後使用backgroundTintbackgroundTintMode屬性查看:

 <View 
      android:backgroundTint="#FF00FF00" 
      android:backgroundTintMode="multiply" 
      .... 

在這個例子中,它是綠色的。

KitKat及更早,這些屬性是不存在的,所以你將不得不求助於代碼:

 setColorFilter(Color.GREEN, Mode.MULTIPLY); 

Here's a tutorial for this method.

相關問題