Android DrawableTint使用

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也可以这么用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值