2016-10-10 90 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.

相关问题