Set style for TextView programmatically

后端 未结 12 1651
南方客
南方客 2020-11-22 09:47

I\'m trying to use the TextView constructor with style like this:

TextView myText = new TextView(MyActivity.this, null, R.style.my_style);


        
相关标签:
12条回答
  • 2020-11-22 10:25
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
        textView.setTextAppearance(R.style.yourStyle)
    
    0 讨论(0)
  • 2020-11-22 10:26

    Parameter int defStyleAttr does not specifies the style. From the Android documentation:

    defStyleAttr - An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

    To setup the style in View constructor we have 2 possible solutions:

    1. With use of ContextThemeWrapper:

      ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.your_style);
      TextView textView = new TextView(wrappedContext, null, 0);
      
    2. With four-argument constructor (available starting from LOLLIPOP):

      TextView textView = new TextView(yourContext, null, 0, R.style.your_style);
      

    Key thing for both solutions - defStyleAttr parameter should be 0 to apply our style to the view.

    0 讨论(0)
  • 2020-11-22 10:27

    I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is a template"
            style="@style/my_style" />
    

    then inflate this to instantiate your new TextView:

    TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 10:27

    When using custom views that may use style inheritance (or event styleable attributes), you have to modify the second constructor in order not to lose the style. This worked for me, without needing to use setTextAppearence():

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, attrs.getStyleAttribute());
    }
    
    0 讨论(0)
  • 2020-11-22 10:27

    We can use TextViewCompact.setTextAppearance(textView, R.style.xyz).

    Android doc for reference.

    0 讨论(0)
  • 2020-11-22 10:33

    I met the problem too, and I found the way to set style programatically. Maybe you all need it, So I update there.

    The third param of View constructor accepts a type of attr in your theme as the source code below:

    public TextView(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.textViewStyle);
    }
    

    So you must pass a type of R.attr.** rather than R.style.**

    In my codes, I did following steps:

    First, customize a customized attr to be used by themes in attr.xml.

    <attr name="radio_button_style" format="reference" />
    

    Second, specific your style in your used theme in style.xml.

     <style name="AppTheme" parent="android:Theme.Translucent">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="radio_button_style">@style/radioButtonStyle</item>
    </style>
    <style name="radioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">64dp</item>
        <item name="android:background">#000</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:saveEnabled">false</item>
        <item name="android:textColor">@drawable/option_text_color</item>
        <item name="android:textSize">9sp</item>
    </style>
    

    At the end, use it!

                RadioButton radioButton = new RadioButton(mContext, null, R.attr.radio_button_style);
    

    the view created programatically will use the specified style in your theme.

    You can have a try, and hope it can work for you perfectly.

    0 讨论(0)
提交回复
热议问题