【问题标题】:How to change color of CompoundDrawable on Button?如何在按钮上更改 CompoundDrawable 的颜色?
【发布时间】:2016-12-19 22:47:15
【问题描述】:

我正在尝试弄清楚如何更改按钮左侧可绘制图标的颜色。

下面是我正在使用的 XML 代码:

  <Button
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_toRightOf="@+id/student_images"
        android:drawableLeft="@mipmap/ic_email_black_18dp"
        android:text="   myemail@gmail.com  "
        android:layout_below="@+id/email"
        android:background="#00000000"
        android:layout_marginBottom="20dp"
        android:fontFamily="sans-serif"
        android:textColor="@color/gray_text_color"
        />

我尝试了android:tint,但图标的颜色没有改变。我被困在这里。

【问题讨论】:

    标签: android android-drawable android-icons


    【解决方案1】:

    我一直在寻找您在这里提出的问题,最后,我找到了一个hack,而不是一个解决方案,在该解决方案中,我决定拥有两个具有所需配置的可绘制对象,并将它们分别设置在需要时编码。

    想象一下,我想将可绘制的颜色更改为灰色以表明它已被停用。这是正常情况下的drawable: ic_email_black_18dp.xml

    <vector 
        android:height="18dp" 
        android:viewportHeight="24.0"
        android:viewportWidth="24.0" 
        android:width="18dp" xmlns:android="http://schemas.android.com/apk/res/android">
        <path 
        android:fillColor="#FF000000"  
        android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
    </vector>
    
    

    你只是想改变fillColor,所以这里是新的XML文件:ic_email_gray_18dp.xml

    <vector 
        android:height="18dp" 
        android:viewportHeight="24.0"
        android:viewportWidth="24.0" 
        android:width="18dp" xmlns:android="http://schemas.android.com/apk/res/android">
        <path 
        android:fillColor="#44000000"  
        android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
    </vector>
    
    

    现在在您的代码中,您可以通过用新的完全替换它来更改可绘制对象的颜色:

            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
    
                            //Everything else that this button is supposed to do
    
                            button.setCompoundDrawablesWithIntrinsicBounds(
                                 R.drawable.ic_email_gray_18dp, //left
                                 0, //top
                                 0, //right
                                 0 //bottom
                                   );
    
                }
            });
    

    这个解决方案听起来很有希望,因为复制的文件几乎不占用空间,而且您可以对图标的不同状态进行更多自定义,而无需执行大量不必要的样板代码(当您需要更改的不仅仅是图标中的单色等)。

    【讨论】:

      【解决方案2】:

      您也可以将其用于 XML

      app:drawableTint="@android:color/white"
      

      【讨论】:

        【解决方案3】:

        设置图标颜色使用

        android:drawableTint="@color/colorPrimary"
        

        【讨论】:

        • 不错的方法,可惜只能在 SDK 23+ 上使用。
        【解决方案4】:

        您可以像下面这样以编程方式设置色调:

        int tintColor = ContextCompat.getColor(context, android.R.color.darker_gray);
        
        Button button = (Button) findViewById(R.id.button);
        
        Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.ic_email_black_18dp);
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable.mutate(), tintColor);
        
        drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        
        button.setCompoundDrawables(drawable, null, null, null);
        

        或者您可以使用库 Support Drawable Tints by snodnipper。
        该库可以为按钮的 drawableLeft 设置色调。
        https://github.com/snodnipper/android-appcompat-issue198613

        【讨论】:

        • 感谢您的回复,但是 tint 无法与 Drawable left 一起使用,但它与 drawable 一起使用效果很好。我怎样才能改变drawableLeft的颜色
        • 我更新了我的答案。上面的代码改变了drawableLeft的颜色。请将第一行更改为您的色调颜色,并使用上面的代码。
        • 我知道这是一篇旧帖子,但是当我使用 setCompoundDrawablesWithIntrinsicBounds 时,图标下方显示了文字。通过您的修复,它确实改变了矢量图标的颜色,但它(图标)显示在左侧。可以在底部显示吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 2019-08-25
        相关资源
        最近更新 更多