可以执行以下操作:
if (colorScheme == 1)
button.setBackgroundResource(R.drawable.button + "_1")
如果在可绘制文件夹中有名为button_1.png,button_2.png,button_3.png的文件,则使用R.drawable.button_1作为配色方案1中此按钮的资源。 (根据使用的配色方案,为同一个UI元素动态使用不同的资源文件?)
谢谢,
答案 0 :(得分:2)
我使用getIdentifier()做了类似的事情:
int resId = context.getResources().getIdentifier("button_1","drawable",context.getPackageName());
button.setBackgroundResource(resId);
答案 1 :(得分:1)
为了使它成为动态的,需要一些代码。您可以在xml中设置布局,如下所示:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
然后在你的代码中引用它:
int resId = context.getResources().getIdentifier("button_1","drawable",context.getPackageName());
Button button = (Button view.findViewById(R.id.button1);
button.setBackgroundResource(resId);
我没有对此进行测试,但这应该会给你一个想法。
答案 2 :(得分:0)
将R.drawable.button_n
放入数组int res[]
,然后按button.setBackgroundResource(res[i])