开发者

Workaround for not been able to set style dynamically

开发者 https://www.devze.com 2023-04-11 08:23 出处:网络
I need to be able to do something like this: if(condition){ textview.setStyle(R.styles.a1); }else{ textview.setStyle(R.styles.a2);

I need to be able to do something like this:

if(condition){
textview.setStyle(R.styles.a1);
}else{
textview.setStyle(R.styles.a2);
}

the thing is you c开发者_C百科an't change the style at runtime, there is methods like setTextApperience and similar but not for setting the style. My question is how to set different backgrounds for the textview of some other view according to a theme, for example in themeA the color of the textview with id=tv1 is blue and for themeB the color of the textview with id=tv1 is red...

I know that this things are discussed on the forum but I couldn't find the thing I need

Edit1: I know that I can set a theme at runtime somebackground...

But my problem is how to set different backgorund for different theme for some specific element , let say element with id=textview1


you can't set the style because their is no such method was defined. but you can create the different theme in xml file and change at runtime

for e.g.

setTheme(android.R.style.Theme_Dark);

so make theme in style and used like this way

Edit:

you can set the background and textcolor like this way

if(condition){
    textview.setBackground(Color.Grey);
    textview.setTextColor(Color.Black);
}else{
    textview.setBackground(Color.Blue);
    textview.setTextColor(Color.White);
}

or check this post answer How to change a TextView's style at runtime

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号