【发布时间】:2017-01-31 09:25:01
【问题描述】:
我有这个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);
它会移除可绘制的形状。我只想更改该特定按钮的颜色。但如何?
谢谢。
【问题讨论】:
-
虽然对于 TextViews,我认为这符合您的需求:-
(GradientDrawable) view.getBackground()).setColor(color);
标签: android