Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓按钮可拉深色调_Android_Android Layout_Drawable_Uicolor - Fatal编程技术网

Android 安卓按钮可拉深色调

Android 安卓按钮可拉深色调,android,android-layout,drawable,uicolor,Android,Android Layout,Drawable,Uicolor,是否可以在android按钮中为drawableLeft着色?我有一件黑色的拖鞋,我想把它染成白色。我知道如何用左边的图像视图来实现这一点,但我想用默认的安卓按钮来实现 我的源代码: <Button android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="@string/drawer_quiz

是否可以在android按钮中为drawableLeft着色?我有一件黑色的拖鞋,我想把它染成白色。我知道如何用左边的图像视图来实现这一点,但我想用默认的安卓按钮来实现

我的源代码:

<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"
        />

有没有办法给按钮上色?或者是否有自定义视图方法来实现此效果?

您可以使用此方法在按钮上为drawableleft着色:

步骤1: 创建一个可绘制的资源文件,使用位图作为父元素,如下所示,并将其命名 作为可绘制文件夹下的ic_action_scanner.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" />
该按钮从可绘制文件夹(而不是@android:drawable或drawable png)的ic_action_scanner.xml中获取可绘制文件

方法2: 步骤1: 您甚至可以将图标添加为操作栏,并使用前景作为图像添加选项卡图标 可以从自定义位置或剪贴画导入的

步骤2: 在“主题”下拉列表下,选择“自定义”

步骤3: 然后在前景颜色选择中选择颜色作为FFFFFF。

最后完成添加图像的向导,然后将可绘制图像添加为图像


正如@Boris所建议的,您可以使用支持库

我已经用一些扩展了AppCompatButton。应该具有默认行为,但用于演示自定义着色

我会看看我是否能提出一个公关,让它进入官方支持图书馆

有关守则如下:

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

我可能会在这方面迟到,但如果您使用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;
希望这有帮助。这是我找到的最简单的方法。
另外,请注意,在应用程序中使用此图标的任何位置,都将是此颜色。

我知道已经有很多答案,但没有一个答案让我很高兴,因为我不想为不同样式的元素使用不同的绘图功能

所以我的解决方案是在构造函数中设置颜色过滤器,如下所示:

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的属性。

您可以在23 android版本中使用android:drawableTint=@color/yourColor。

我可以使用material support library中的一个属性来完成同样的事情,大多数人可能已经在使用它了。有一个名为icon的属性,可以放置在左/中/右默认为左。它还有一个名为IContent的属性,该属性将为图标着色

格拉德尔:

implementation "com.google.android.material:material:1.1.0-alpha09"
视图:


您可以使用drawable过滤器,或者如果API>=M,则可以简单地

textView.compoundDrawableTintList = ColorStateList.valueOf(Color.WHITE)
或者在XML中

android:drawableTint="@color/white"

那么你想在按钮文本的左侧放置一个图像吗?@HawraaKhalil是的,没错。但是如果你想让drawableTint进入支持库,这里应该是白色的。谢谢,我现在使用第一种方法,因为我还想在ui的其他部分使用黑色图标。但是请记住,tint属性只在棒棒糖之后才受支持。对于棒棒糖前的支持,我使用以下代码以编程方式对按钮drawable着色:drawable[]drawables=postButton.getCompoundDrawables;Drawable wrapDrawable=DrawableCompat.wrapdrawables[0];DrawableCompat.setTintwrapDrawable,getResources.getColorandroid.R.color.white;,使用support-v4库。方法1似乎只适用于android的默认图标。如果我们在我们的资源上添加一个新图标,它在我这方面就不起作用。请注意,android:backgroundTint=@color/md_light_green_500它只在api级别23以上,在Lolipopthas中不起作用。我的应用程序的最低版本是21,所以不必担心低于21,那么低于23呢?因为你可以使用app:drawableTint安卓:drawableTint是这里的正确答案。谢谢
android:drawableTint="@color/white"