0

I have some customized Views like:

public class CustomizedTextView extends TextView {
...
    public CustomizedTextView(Context context) {
        this.setText(TEXT_COLOR);
        this.setTextSize(TEXT_SIZE);
        this.setTypeFace(TYPE_FACE);
        this.setPadding(LEFT, TOP, RIGHT, BOTTOM);
        ...
    }
}

As you can see, these style settings and corresponding constants takes many lines of code. So I want to separate it out in resource files.

I have gone through the Android documentation, it says you could defined a style file like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

But I didn't find an API like setStyle(R.style.style001) to set the style in code. I also found a post here: android set style in code

Basically it says you can't do it in code. However this post is three years ago I am not sure what's the situation in API 19. Because defining a customized View is so common in Android that I don't understand why this is not possible.

Community
  • 1
  • 1
darklord
  • 5,077
  • 12
  • 40
  • 65

1 Answers1

0

You can set the textview style using setTextAppearance like this

  textView.setTextAppearance(context, R.style.CodeFont);
Libin
  • 16,967
  • 7
  • 61
  • 83