简体   繁体   中英

Android : set TextView style at runtime

Does any body know how to set style for TextView at run time:

something like this

myTextView.setStyle(R.style.mystyle);

使用setTextApparence和你的风格非常简单

 myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);

you will have to manually set each element of the style that you change, there is no way to setStyle at run time, AFAIK.

myTextView.setTextAppearance
myTextView.setTextSize
myTextView.setTextColor

I also still did not find (sadly) a way to change Style at runtime.

If it is just about changing the checkbox appearance (as you mention in a comment of another answer), you can use this:

myCheckbox.setButtonDrawable(R.drawable.star_checkbox);

And have a star_checkbox.xml file in the drawable directory describing the checkbox background according to its states such as:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_focused="true"
        android:drawable="@drawable/star_checkbox_checked_focused" />

    <item android:state_checked="false" android:state_focused="true"
        android:drawable="@drawable/checkbox_not_checked_focused" />

    <item android:state_checked="false" 
        android:drawable="@drawable/checkbox_not_checked" />

    <item android:state_checked="true" 
        android:drawable="@drawable/checkbox_checked" />
</selector>

Also you need the corresponding png files in you drawable directory.

TextView (Context context, AttributeSet attrs, int defStyle)

Although there is construction available but it seems its buggy, I tried this and found specified style won't apply on my view.

After searching further got this filed bug: http://code.google.com/p/android/issues/detail?id=12683

To workaround this issue I am using setBackgroundResource, setTextAppearance, etc methods dramatically :)

I am trying myself to do a similar thing.

My reason is that I want to use a style from my own Theme, BUT my User Interface Layout is entirely generated in code( using a custom layout builder), without defining any widgets in XML. So I cannot set a style in the XML layout of my widget – there isn't any XML layout.

I am thinking that I will be able to set this style in the code of my widget by using

TypedArray a =

context.obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

Here it seems (to me) that

  1. AttributeSet set = null; because this is what the XML inflater would have provided.

  2. int[] attrs = R.styleable.MyWidget; defines what attributes I want to look at.

  3. int defStyleAttr = myWidgetStyle; which is a reference, defined in my Theme, to a style for MyWidget. These are both defined in XML files in res/values. “myWidgetStyle” follows the pattern of name the android developers have used in their code.

  4. defStyleRes = 0; I am hoping that I don't need to think about this.

Then to get any property , such as a background color,

Color color = a.getColor(R.styleable.MyWidget_background, R.color.my_default);

a.recycle();

This does seem to work –so far anyway.

It seems that the android build system conveniently generates the correct index to use in a.getColor, and names it R.styleable.MyWidget_background . I didn't make this name myself so Android must have done it using my XML for my styleable MyWidget.

I expect one can look up the correct index by searching the TypedArray for the required attribute , but that would be inefficient and the TypedArray looks like an unpleasant contraption to deal with. I would use a very long stick to poke it!

Don

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM