[英]Dynamically change text color of all items in ListView
I want to change my activity theme dynamically without recreating activity, the only solution I've found is to change items properties (like background, textColor) in place, but I have a ListView
with number of items, I can iterate through all items in ListView
and change textColor
, but I think it's kinda an ugly solution. 我想动态更改我的活动主题而不重新创建活动,我发现的唯一解决方案是更改项目属性(如background,textColor),但我有一个包含项目数量的ListView
,我可以遍历所有项目ListView
和更改textColor
,但我认为这是一个丑陋的解决方案。
Is there any better way to achieve this? 有没有更好的方法来实现这一目标?
Thank you in advance 先感谢您
You could do something like this in your Adapters getView()
method: 您可以在Adapters getView()
方法中执行以下操作:
int theme = getThemeColorFromPrefrences();
if(theme == darkTheme){
textView.setTextColor(...);
}
else{...}
Now every time the user changes the color, you just call invalidateViews()
on your ListView
: 现在每次用户更改颜色时,只需在ListView
上调用invalidateViews()
:
// set other theme color [...]
myListView.invalidateViews()
I assume you have custom arraylist of object, on that put a private int color 我假设你有自定义的对象arraylist,在那上面放一个私有的int颜色
class myobject {
private int color;
public void setColor(int color) { this.color=color; }
public int getColor { return color; }
}
on your custom ArrayAdapter List in getView() set 在getView()集中的自定义ArrayAdapter列表中
((TextView)view.findViewById(R.id.textview)).setColor(item.getColor());
//Where item is your MyObject at ArrayList(Position)
When you want to change color just, on your onClick button Black 当您想要改变颜色时,在onClick按钮上黑色
onClick() {
for(int i=0;i<arraylist.size();i++) { arraylist.get(i).setColor(Color.Black); }
arrayadapter.notifydatachange();
}
您可以将自己的布局用于ListView行,这样您就可以在布局文件中指定文本颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.