Android Button Drawable Tint [英] Android Button Drawable Tint
问题描述
是否可以在android按钮中着色drawableLeft?我有一个黑色的绘图板,想要将白色着色.我知道如何使用图片视图(左侧的图片)实现此目的,但我想使用默认的android按钮来实现此目的.
Is it possible to tint the drawableLeft in an android button? I have a black drawable I'd like to tint white. I know how to achieve this with an image view (image on the left) but I want to do this with the default android button.
我的源代码:
<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"
/>
有什么方法可以给按钮着色吗?还是有一种自定义视图方法可以达到这种效果?
Is there any way to tint the button? Or is there a custom view method to achieve this effect?
推荐答案
您可以使用以下方法为按钮上的drawableleft着色:
You can achieve coloring the drawableleft on a button with this method:
第1步:
创建一个以位图为父元素的可绘制资源文件,如下所示,并将其命名
为ic_action_landscape.xml
在可绘制文件夹下
Step 1:
Create a drawable resource file with bitmap as parent element as shown below and name it
as ic_action_landscape.xml
under the drawable folder
<?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按钮,如下所示:
Step 2: Create your Button control in your layout as below
<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" />
按钮从可绘制文件夹中的ic_action_landscape.xml
中获取可绘制对象,而不是@android:drawable或drawable png.
The button gets the drawable from the ic_action_landscape.xml
from the drawable folder instead of @android:drawable or drawable png(s).
方法2:
步骤1:
您甚至可以将图标添加为动作栏"和选项卡"图标,并将前景"作为图像添加
可以从自定义位置或剪贴画导入
Method 2:
Step 1:
You can even add the icon as a Action Bar and Tab icons with Foreground as Image
that can be imported from a custom location or a Clipart
第2步:
在主题下拉列表中,选择自定义
Step 2:
Under Theme dropdown select Custom
第3步:
然后在前景颜色"选择中将颜色选择为#FFFFFF.
Step 3:
Then select the color as #FFFFFF in the Foreground color selection.
最后完成向导以添加图像,然后将可绘制对象添加为图像.
Finally finish the wizard to add the image, then add the drawable as an image.
这篇关于Android Button Drawable Tint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!