我想动态更改我的活动主题而不重新创建活动,我发现的唯一解决方案是更改项目属性(如background,textColor),但我有一个ListView
,其数量为项目,
我可以遍历ListView
中的所有项目并更改textColor
,但我认为这有点难看。左右。
有没有更好的方法来实现这个目标?
提前谢谢
答案 0 :(得分:2)
您可以在适配器getView()
方法中执行以下操作:
int theme = getThemeColorFromPrefrences();
if(theme == darkTheme){
textView.setTextColor(...);
}
else{...}
现在,每当用户更改颜色时,您只需在invalidateViews()
上致电ListView
:
// set other theme color [...]
myListView.invalidateViews()
答案 1 :(得分:1)
我假设你有自定义的对象arraylist,就放了一个私有的int颜色
class myobject {
private int color;
public void setColor(int color) { this.color=color; }
public int getColor { return color; }
}
在getView()set 中的自定义ArrayAdapter列表上
((TextView)view.findViewById(R.id.textview)).setColor(item.getColor());
//Where item is your MyObject at ArrayList(Position)
如果您想改变颜色,请点击onClick按钮黑色
onClick() {
for(int i=0;i<arraylist.size();i++) { arraylist.get(i).setColor(Color.Black); }
arrayadapter.notifydatachange();
}
答案 2 :(得分:0)
您可以为ListView行使用自己的布局,这样就可以在布局文件中指定文本颜色。