不再有意义!这个问题与旧的Android4.x版本中的一个bug有关。
android:tint现在应该可以正常工作,如下面的示例所示
我正在尝试将染色应用于< layer-list >中的位图
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/grey" />
<size android:width="45dp" android:height="45dp"/>
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">
<bitmap android:src="@drawable/ic_action" android:tint="#FF000000" />
</item>
</layer-list>预览显示,它应该可以工作,从ANDROID-STUDIO:

但当部署到设备上时,它不会着色:

如果我在布局中使用ImageView,它可以正确着色,但使用layer-list失败。我相信我已经尝试了所有的tintMode,但都没有结果。
发布于 2017-06-03 07:29:35
对于其他任何可能遇到这个问题的人。这就是我所做的。
设置
Gradle
编译'com.android.support:appcompat-v7:25.3.1'
15
图层可绘制我将android:id=@+id/bitmapID添加到包含位图的项目中
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval">
<solid android:color="#9e9e9e" />
<size android:width="45dp" android:height="45dp"/>
</shape>
</item>
<item
android:id="@+id/tintDrawableImg"
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="5dp">
<bitmap android:src="@mipmap/ic_pencil" android:tint="#860000" />
</item>
</layer-list>活动布局我将可绘制的图层添加到ImageView
<ImageView
android:id="@+id/tintLayerTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/test_layer"/>MainActivity在onCreate()方法中,我们可以使用findViewByID从可绘制的图层中定位位图
public class MainActivity extends AppCompatActivity {
ImageView iv;
LayerDrawable ld;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare ImageView containing LayerDrawable
iv = (ImageView)findViewById(R.id.tintLayerTest);
//Get LayerDrawable from ImageView
ld = (LayerDrawable)iv.getDrawable();
//Get specific Drawable/Bitmap from within LayerDrawable
//by ID and pass it as an independent Drawable
Drawable ldDrawable = ld.findDrawableByLayerId(R.id.tintDrawableImg);
//Pass your new Drawable to DrawableCompat.setTint
//and define your color int
DrawableCompat.setTint(ldDrawable, ContextCompat.getColor(this, R.color.colorAccent));
}
}我希望这对遇到这个问题的其他人有所帮助。
发布于 2019-09-19 14:26:52
尝试使用位图标记进行xml可绘制
保存以下文件xml custom_image.xml并替换您的图标和色调颜色
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/iv_veg"
android:tint="@color/red">
</bitmap>发布于 2021-03-03 07:40:43
正如@lucidbrot在评论中提到的那样,android:tint现在应该可以不受任何干扰地工作了。
https://stackoverflow.com/questions/26524697
复制相似问题