tint意思是“着色”,就是可以改变颜色。
这个在android中应用就可以改变图标或者背景的颜色。好处就是可以节省图标数量,相同图标不同颜色就可以直接用这个属性实现,不需要导入无需的图标资源了。
使用的资源文件
xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatButton
android:text="Tint"
android:textAllCaps="false"
android:drawableTintMode="add"
android:drawableTint="@color/colorWarning"
android:drawableTop="@drawable/ic_message_ok"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
vector资源drawable文件
<vector android:height="36dp" android:viewportHeight="1024"
android:viewportWidth="1024" android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/colorOk" android:pathData="M512,64C264.6,64 64,264.6 64,512s200.6,448 448,448 448,-200.6 448,-448S759.4,64 512,64zM738.3,398.9L466.8,670.4c-12.5,12.5 -32.8,12.5 -45.3,0L285.7,534.6c-12.4,-12.5 -12.4,-32.8 0,-45.3 6.2,-6.2 14.4,-9.3 22.6,-9.3 8.2,0 16.4,3.1 22.6,9.3L444,602.4l249,-248.8c12.4,-12.5 32.8,-12.5 45.3,0 6.2,6.2 9.3,14.4 9.3,22.6 0,8.2 -3.1,16.4 -9.3,22.7z"/>
</vector>
属性效果
1、xml中的一个button按钮:
2、现在添加上drawableTint属性:
3、还有个drawableTintMode属性,可以设置着色的模式:
**相应的backgroundTint,foregroundTint也可以这么用。