从可绘制按钮更改背景颜色

时间:2017-01-31 09:25:01

标签: android

我有这个round_button.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"></solid>
            <corners android:radius="3dp"></corners>
        </shape>
    </item>

</selector>

我使用那个drawable来获取我的两个按钮:

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Button_Register"
            android:background="@drawable/round_button"
            android:textColor="@color/formLabelTextColor"
            android:text="@string/loginform_buttonregister" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Button_Login"
            android:background="@drawable/round_button"
            android:textColor="@color/komWhite"
            android:text="@string/loginform_buttonlogin" />

我的问题是......

如何在java中更改Button_Register背景颜色?我试着用这个:

    Button button_register = (Button) findViewById(R.id.Button_Register);
    button_register.setBackgroundColor(R.color.formBackgroundColor);

并删除可绘制的形状。我只是想改变那个特定按钮的颜色。但怎么样?

谢谢。

5 个答案:

答案 0 :(得分:4)

public static void setDrawableFilterColor(Context context, int colorResource, Drawable drawable) {
    //noinspection ResourceType
    int filterColor = Color.parseColor(context.getResources().getString(colorResource));
    drawable.setColorFilter(new PorterDuffColorFilter(filterColor, PorterDuff.Mode.MULTIPLY));
}

并用

调用它
setDrawableFilterColor(mContext, R.color.colorPrimary, button_register.getBackground())

答案 1 :(得分:3)

您只需使用GradientDrawable这样的

更改xml drawable的背景颜色即可
GradientDrawable gradientDrawable = (GradientDrawable)button_register.getBackground();
gradientDrawable.setColor(R.color.formBackgroundColor);

答案 2 :(得分:2)

在选择器文件中的shape属性上设置drawable颜色,如下所示

<item android:drawable="@color/yourcolor">

完整代码

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary"></solid>
        <corners android:radius="3dp"></corners>      
    </shape>
</item>
</selector>

答案 3 :(得分:0)

您可以通过在运行时使用您需要的颜色创建drawable来实现此目的。以下代码将帮助您以编程方式绘制 -

Button button = (Button) findViewById(R.id.btn_login);
final GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(10);
gradientDrawable.setColor(Color.BLUE);
button.setBackground(gradientDrawable);

希望它对你有所帮助。

答案 4 :(得分:0)

根据sasikumar的答案,这是可用于按钮的矩形代码,请注意:

<solid android:color="@color/colorWhite"></solid>

drawable的完整代码为:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_square">
    <stroke android:width="1dp" android:color="#3f3f44" />
    <padding android:left="2dp"
    <solid android:color="@color/colorWhite"></solid>
    android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="10dp" />
</shape>

这就是我的实现方式:

 <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="5dp"
    android:background="@drawable/buttonsquare"
    android:text="Button text"
    android:layout_marginStart="5dp">
 </Button>
相关问题