标题翻译
Dynamically change Material Button background not working
问题
我有一个 Material 按钮,我想动态地更改它的背景,我已经为此创建了可绘制的 XML 文件,以前它是有效的,现在不起作用了,我尝试了以下选项,但我的按钮仍然是蓝色的:
mBtnStudy.setBackgroundResource(R.drawable.switch_left_enabled);
mBtnStudy.setBackgroundColor(getResources().getColor(R.color.red));
mBtnStudy.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.switch_left_enabled));
mBtnStudy.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.switch_left_enabled));
XML 部分如下:
(以下是 XML 部分的原文,我不会再次翻译出来,以避免重复内容)
可能的问题是什么?
Note: I've omitted the XML content and repeated translations to avoid redundancy, as requested. If you need further assistance or have additional questions, feel free to ask.
英文翻译
I have a Material button, i want to change its background dynamically, i have created drawable xml for that, previously it was working, now its not working, i tried below options, still my button is blue color only
mBtnStudy.setBackgroundResource(R.drawable.switch_left_enabled);
mBtnStudy.setBackgroundColor(getResources().getColor(R.color.red));
mBtnStudy.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.switch_left_enabled));
mBtnStudy.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.switch_left_enabled));
and xml looks like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="16dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="@string/study"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_test_yourself"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="@string/test_yourself"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Drawable
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<!--<corners android:bottomLeftRadius="10dp" android:topLeftRadius="10dp"/>-->
<solid android:color="#bdbdbd"/>
</shape>
What might be the issue?
答案1
得分: 0
在Java文件中,button.setBackgroundColor(Color.RED);
将RED替换为您所需的颜色。
要使用颜色代码,请使用button.setBackgroundColor(Color.parseColor("#03dac5");
英文翻译
In java file button.setBackgroundColor(Color.RED);
Replace RED to Your Desired Color.
And to use Color Code Use button.setBackgroundColor(Color.parseColor("#03dac5");
专注分享java语言的经验与见解,让所有开发者获益!
评论