【问题标题】:Change Colors States of Button using a Generic Drawable使用通用可绘制对象更改按钮的颜色状态
【发布时间】:2018-03-21 13:49:27
【问题描述】:

我有 Button 使用自定义可绘制来制作圆角。但我想改变不同状态的背景颜色,聚焦,按下等。

我在 onCreate 中使用了以下内容来更改整体背景颜色。但我不知道如何定位state_pressed 来改变它的颜色。

有什么帮助吗?

在 onCreate 中

DrawableCompat.setTint(btnLogin.getBackground(), ContextCompat.getColor(getApplicationContext(), R.color.colorGreen));

按钮

 <Button
            android:id="@+id/btnLogin"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/button_rounded"
            android:text="Sign In"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

button_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="10dip" />

            <solid android:color="#5c89c1" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <corners android:radius="10dip" />

            <solid android:color="#204778" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dip" />

            <solid android:color="#204778" />
        </shape>
    </item>
</selector>

【问题讨论】:

  • 版本API,你在用吗?
  • 23 棉花糖及以上

标签: java android android-drawable


【解决方案1】:

试试这个,它对我有用:

Drawable drawable = getResources().getDrawable(R.drawable.button_rounded);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.colorGreen));

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多