我试图让用户通过单选按钮更改我的应用程序的颜色。用户单击单选按钮,然后调用一个方法。现在,我想将5个xml文件的背景颜色更改为x / y / z。但是我不知道应该如何引用xml文件本身,因为它们没有id。我在字符串资源中有5种颜色,因此当我可以引用xml文件时,背景颜色将被更改对于所有文件。(颜色的十六进制表示法替换字符串)。可以做到吗,还是我必须回过头来再次旋转呢?
public void rbbgColourClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.rbbgcolour_grey:
if (checked)
// Change to grey
break;
case R.id.rbbgcolour_blue:
if (checked)
// Change to blue
break;
case R.id.rbbgcolour_white:
if (checked)
// Change to white
break;
}
}
如果我放进去
LinearLayout one = (LinearLayout) findViewById(R.layout.preferences);
one.setBackgroundColor(0xff888888);
不明白为什么它在那里。
谢谢
您可以使用SharedPreferences执行此操作。您可以将背景色十六进制代码存储为首选项。用户单击单选按钮时,必须在SharedPreferences中更改十六进制代码。是这样的...
在每个活动的onCreate中...
SharedPreferences sp = getSharedPreferences("MyPref", 0);
String hexaColor = sp.getString("hexa", "#000000"); //default color will be #000000
然后将此设置为这些活动的背景色。
当用户单击单选按钮时,请执行此操作...
SharedPreferences sp = getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("hexa", "new hexa code");
editor.commit();
希望这会帮助你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句