如何在Button上更改CompoundDrawable的颜色?

时间:2016-08-12 20:38:48

标签: android android-drawable android-icons

我试图找出如何更改按钮左侧可绘制图标的颜色。

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

color:rgba(0,0,0,0)

我尝试了 <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" /> ,但图标颜色没有变化。我被困在这里。

4 个答案:

答案 0 :(得分:6)

您可以通过编程方式设置色调,如下所示:

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);

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

答案 1 :(得分:2)

设置图标颜色使用

android:drawableTint="@color/colorPrimary"

答案 2 :(得分:0)

您也可以将其用于 XML

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

答案 3 :(得分:0)

我一直在寻找您在此处提出的问题,最后,我发现了一个 hack ,而不是一种解决方案,在该解决方案中,我决定拥有两个具有所需配置的drawable,并分别在其中设置必要时输入代码。

想象一下,我想将可绘制的颜色更改为灰色,以表明它已被停用。这是正常情况下的绘制: import os import glob import email from email import policy indir = '.' outdir = os.path.join(indir, 'output') os.makedirs(outdir, exist_ok=True) files = glob.glob(os.path.join(indir, '*.eml')) for eml_file in files: # This will not work in Python 2 msg = email.message_from_file(open(eml_file), policy=policy.default) for att in msg.iter_attachments(): # Tabs may be added for indentation and not stripped automatically filename = att.get_filename().replace('\t', '') # Here we suppose for simplicity sake that each attachment has a valid unique filename, # which, generally speaking, is not true. with open(os.path.join(outdir, filename), 'wb') as f: f.write(att.get_content())

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> ,所以这是新的XML文件:fillColor

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>

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