Android - set TextView TextStyle programmatically?

前端 未结 11 570
南旧
南旧 2020-12-02 05:04

Is there a way to set the textStyle attribute of a TextView programmatically? There doesn\'t appear to be a setTextStyle() method.

相关标签:
11条回答
  • 2020-12-02 05:37

    Let's say you have a style called RedHUGEText on your values/styles.xml:

    <style name="RedHUGEText" parent="@android:style/Widget.TextView">
        <item name="android:textSize">@dimen/text_size_huge</item>
        <item name="android:textColor">@color/red</item>
        <item name="android:textStyle">bold</item>
    </style>
    

    Just create your TextView as usual in the XML layout/your_layout.xml file, let's say:

    <TextView android:id="@+id/text_view_title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content 
        android:text="FOO" />
    

    And in the java code of your Activity you do this:

    TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
    textViewTitle.setTextAppearance(this, R.style.RedHUGEText);
    

    It worked for me! And it applied color, size, gravity, etc. I've used it on handsets and tablets with Android API Levels from 8 to 17 with no problems. Note that as of Android 23, that method has been deprecated. The context argument has been dropped, so the last line would need to be:

    textViewTitle.setTextAppearance(R.style.RedHUGEText);
    

    Remember... this is useful only if the style of the text really depends on a condition on your Java logic or you are building the UI "on the fly" with code... if it doesn't, it is better to just do:

    <TextView android:id="@+id/text_view_title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content 
        android:text="FOO" 
        style="@style/RedHUGEText" />
    

    You can always have it your way!

    0 讨论(0)
  • 2020-12-02 05:37

    This question is asked in a lot of places in a lot of different ways. I originally answered it here but I feel it's relevant in this thread as well (since i ended up here when I was searching for an answer).

    There is no one line solution to this problem, but this worked for my use case. The problem is, the 'View(context, attrs, defStyle)' constructor does not refer to an actual style, it wants an attribute. So, we will:

    1. Define an attribute
    2. Create a style that you want to use
    3. Apply a style for that attribute on our theme
    4. Create new instances of our view with that attribute

    In 'res/values/attrs.xml', define a new attribute:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr name="customTextViewStyle" format="reference"/>
        ...
    </resources>    
    

    In res/values/styles.xml' I'm going to create the style I want to use on my custom TextView

    <style name="CustomTextView">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingLeft">14dp</item>
    </style>
    

    In 'res/values/themes.xml' or 'res/values/styles.xml', modify the theme for your application / activity and add the following style:

    <resources>
        <style name="AppBaseTheme" parent="android:Theme.Light">
            <item name="@attr/customTextViewStyle">@style/CustomTextView</item>
        </style>
        ... 
    </resources>
    

    Finally, in your custom TextView, you can now use the constructor with the attribute and it will receive your style

    public class CustomTextView extends TextView {
    
        public CustomTextView(Context context) {
           super(context, null, R.attr.customTextView);
        }
    }
    

    It's worth noting that I repeatedly used customTextView in different variants and different places, but it is in no way required that the name of the view match the style or the attribute or anything. Also, this technique should work with any custom view, not just TextViews.

    0 讨论(0)
  • 2020-12-02 05:38

    I´ve resolved it with two simple methods.

    Follow the explanation.

    My existing style declaration:

    <style name="SearchInfoText">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">24sp</item>
        <item name="android:textColor">@color/Church_Grey</item>
        <item name="android:shadowColor">@color/Shadow_Church</item>
        <item name="android:shadowRadius">3</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
    </style>
    

    My Android Java code:

        TextView locationName = new TextView(getSupportActivity());
        locationName.setId(IdGenerator.generateViewId());
        locationName.setText(location.getName());
        locationName.setLayoutParams(super.centerHorizontal());
        locationName.setTextSize(24f);
        locationName.setPadding(0, 0, 0, 15);
        locationName.setTextColor(getResources().getColor(R.color.Church_Grey));
        locationName.setShadowLayer(3, 1, 1,  getResources().getColor(R.color.Shadow_Church));
    

    Regards.

    0 讨论(0)
  • 2020-12-02 05:41

    Since setTextAppearance(resId) is only available for API 23 and above, use:

    TextViewCompat.setTextAppearance(textViewGoesHere, resId)

    This method is internally implemented as follows:

    public static void setTextAppearance(@NonNull TextView textView, @StyleRes int resId) {
        if (Build.VERSION.SDK_INT >= 23) {
            textView.setTextAppearance(resId);
        } else {
            textView.setTextAppearance(textView.getContext(), resId);
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:44

    Kotlin Version

    To retain current font in addition to text style:

    textView.apply {
        setTypeface(typeface, Typeface.NORMAL)
        // or
        setTypeface(typeface, Typeface.BOLD)
        // or
        setTypeface(typeface, Typeface.ITALIC)
        // or
        setTypeface(typeface, Typeface.BOLD_ITALIC)
    }
    
    0 讨论(0)
  • 2020-12-02 05:48

    You may try this one

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    textView.setTextAppearance(R.style.Lato_Bold);
                } else {
                    textView.setTextAppearance(getActivity(), R.style.Lato_Bold);
                }
    
    0 讨论(0)
提交回复
热议问题