假设我希望用户更改应用的背景颜色。我正在考虑创建几个xml布局文件,然后使用包含布局文件列表的ListView。然后,当用户选择其中一个时,将加载相应的xml文件。
这可能吗?如果是,我应该怎么做?
答案 0 :(得分:4)
最简单的方法是在代码中动态化。只需将用户颜色保存到SharedPreferences,并在代码中将其用作视图的背景。
所以我建议使用ColorPikerDialog来选择任何颜色。您可以使用此库https://github.com/gsingh93/android-ColorPickerPreference
SharedPrefrences prefs = getSharedPrefrences(YOUR_SHARED_PREFS, 0);
int colorId = prefs.getInt(BACKGROUND_COLOR, 0);
if(colorId != 0) {
setBackgroundToColorId(colorId);
}
祝你好运!
答案 1 :(得分:1)
我已经这样做了,当用户从listview轨道中选择colorid并将其保存在共享首选项中时,@ llya Demidov说。
editor = getSharedPreferences(PREFS_NAME, 0).edit();
editor.putString(PREF_COLOR, <userselectedcolor>);
在加载每个活动之前,请执行此操作
pref = getSharedPreferences(PREFS_NAME, 0);
int color= pref.getString(PREF_COLOR, null);
if(color!=0)
{
yourlayoutid.setbackgroundColor(Color);
}
答案 2 :(得分:0)
仅将此事视为建议,
您可以放置一个edittext,其中包含用户想要的颜色代码,您可以使用TextWatcher,因为颜色具有十六进制值,每种颜色的修复长度为6个字母数字字符,如“FF0000”,长度变为六,颜色将适用。
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable editable)
{
if(editable.toString().length==6)
//set color of background
}
});
}