我有这个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>
Run Code Online (Sandbox Code Playgroud)
我将那个 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" />
Run Code Online (Sandbox Code Playgroud)
我的问题是...
如何Button_Register在java中更改背景颜色?我试着用这个:
Button button_register = (Button) findViewById(R.id.Button_Register);
button_register.setBackgroundColor(R.color.formBackgroundColor);
Run Code Online (Sandbox Code Playgroud)
它删除了可绘制的形状。我只想更改该特定按钮的颜色。但是怎么样?
谢谢你。
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));
}
Run Code Online (Sandbox Code Playgroud)
并调用它
setDrawableFilterColor(mContext, R.color.colorPrimary, button_register.getBackground())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5949 次 |
| 最近记录: |