2015-06-19 61 views
17

是否可以在android按钮中为drawableLeft着色?我有一个黑色的drawable,我想要着色为白色。我知道如何通过图像视图来实现这一点(图像左侧),但我想用默认的android按钮来实现。Android按钮可绘制色调

enter image description here

我的源代码:

<Button android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:text="@string/drawer_quizzes" 
     android:backgroundTint="@color/md_light_green_500" 
     android:stateListAnimator="@null" 
     android:textColor="#fff" 
     android:textSize="12dp" 
     android:fontFamily="sans-serif" 
     android:drawableLeft="@drawable/ic_action_landscape" 
     android:gravity="left|center_vertical" 
     android:drawablePadding="8dp" 
     /> 

有什么办法来着色的按钮?或者是否有自定义视图方法来实现这种效果?

+0

所以你想要在按钮的文本左侧放置图像? –

+0

@HawraaKhalil确实如此。但它应该是有色的白色 –

+1

在这里投票https://code.google.com/p/android/issues/detail?id=198613如果你想drawableTint进入支持库 –

回答

38

可以实现着色上用此方法一个按钮drawableleft:

步骤1: 创建位图作为父元素的可绘制资源文件作为可绘制文件夹下如下所示,并将其命名 作为ic_action_landscape.xml

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@android:drawable/ic_btn_speak_now" 
    android:tint="@color/white" /> 

第2步: 在布局如下

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:backgroundTint="@color/md_light_green_500" 
     android:drawableLeft="@drawable/ic_action_landscape" 
     android:drawablePadding="8dp" 
     android:fontFamily="sans-serif" 
     android:gravity="left|center_vertical" 
     android:stateListAnimator="@null" 
     android:text="@string/drawer_quizzes" 
     android:textColor="#fff" 
     android:textSize="12dp" /> 
012创建按钮控件

该按钮从可绘制文件夹而不是@android:drawable或drawable png(s)从ic_action_landscape.xml获取drawable。

方法2:
步骤1:
您甚至可以添加的图标为一个动作栏和标签的图标,前景为图像 可以从自定义位置或剪贴画

第2步导入:
在主题下拉列表中选择自定义

步骤3:
然后在前景颜色选择中选择颜色为#FFFFFF。 Method 2 Pictorial representation

最后完成向导添加图像,然后将绘图添加为图像。

Pictorial representation for answer to the question

+0

谢谢,我现在使用第一种方法因为我也想在UI的其他部分使用(黑色)图标。 –

+14

请注意,tint属性仅支持自棒棒糖以来。对于Lollipop之前的支持,我使用下面的代码来以编程方式绘制按钮drawable:'Drawable [] drawables = postButton.getCompoundDrawables(); \t \t \t Drawable wrapDrawable = DrawableCompat.wrap(drawables [0]); \t \t \t DrawableCompat.setTint(wrapDrawable,getResources()。getColor(android.R.color.white));',使用support-v4库。 – Boris

+0

看来方法1只适用于默认图标(来自android)。如果我们在资源上添加一个新图标,它不适用于我的最终目的。只是一个笔记。 – vida

1

由于@Boris建议,您可以使用支持库。

我已经扩展了一些draft code AppCompatButton。 TintAppCompatButton应具有默认行为,但TintAppCompatButtonCustom被制作为演示自定义着色。

我会看看我是否可以通过公关将其纳入官方支持库。

感兴趣的代码是:

private void init() { 
    PorterDuff.Mode tintMode = PorterDuff.Mode.SRC_IN; 
    Drawable[] ds = getCompoundDrawables(); 
    tint(ds[LEFT], Color.RED, tintMode); 
    tint(ds[TOP], Color.YELLOW, tintMode); 
    tint(ds[RIGHT], Color.GREEN, tintMode); 
    tint(ds[BOTTOM], Color.BLUE, tintMode); 
} 

private void tint(Drawable d, int color, PorterDuff.Mode tintMode) { 
    TintInfo ti = new TintInfo(); 
    ti.mTintMode = tintMode; 
    ti.mTintList = ColorStateList.valueOf(color); 
    ti.mHasTintList = true; 
    ti.mHasTintMode = true; 

    TintManager.tintDrawable(d, ti, new int[]{0}); 
} 
1

我可能会迟到的这一点,但如果你使用C#,你可以这样来做:

Color iconColor = Color.Orange; // TODO: Get from settings 
Mode mode = Mode.SrcAtop; 

Drawable drawable = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.ic_action_arrest, null); 

drawable.SetColorFilter(iconColor, mode); 

您还需要:

using Android.Graphics; 
using static Android.Graphics.PorterDuff; 
using Android.Graphics.Drawables; 

希望这会有所帮助。这是我找到的最简单的方法。 请注意,在您的应用程序中,您使用此图标的任何地方都是这种颜色。

0

我知道已经有很多答案,但其中没有一个让我开心,因为我不想为不同样式的元素绘制不同的drawable。

所以我的解决办法是设置彩色滤光片的构造是这样的:

int textColor = getTextColors().getColorForState(EMPTY_STATE_SET, Color.WHITE); 
    Drawable[] drawables = getCompoundDrawablesRelative(); 
    for (Drawable drawable : drawables) { 
     if(drawable != null) { 
      drawable.setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP); 
     } 
    } 

我用文字的颜色,因为这是我所需要的,但它可以通过自定义属性来代替更DINAMIC。