How to prevent TextView font changing, when we change the Device font style in Samsung devices

前端 未结 3 1715
醉酒成梦
醉酒成梦 2020-12-11 04:22

I would like say that I have a strange problem in setting textview font. If I change the Device font style from setting-> display, then textview font style also get chang

相关标签:
3条回答
  • 2020-12-11 04:53

    fontFamily and typeface attributes are related to android native fonts. If you want your TextViews font to be always the same regardless the device font settings, you need to programmatically set a custom Typeface.

    Typeface tf = Typeface.createFromAsset(context.getAssets(), "fontName.ttf"));
    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setTypeface(tf);
    

    As a side note, Calligraphy project enables actually setting custom font directly from a layout xml.

    0 讨论(0)
  • 2020-12-11 04:59
    public class CustomTextView extends TextView {
    
        Context context;
    
        public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.context = context;
        }
    
        public CustomTextView (Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context = context;
        }
    
        public CustomTextView (Context context) {
            super(context);
            this.context = context;
        }
    
        public void setTypeface(Typeface tf, int style) {
            if (style == Typeface.NORMAL) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeue.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
            } else if (style == Typeface.ITALIC) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueItalic.ttf")/*
                                                                                                                     * ,
                                                                                                                     * -
                                                                                                                     * 1
                                                                                                                     */);
            } else if (style == Typeface.BOLD) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueBold.ttf")/*
                                                                                                                     * ,
                                                                                                                     * -
                                                                                                                     * 1
                                                                                                                     */);
            }
        }
    
    }
    

    and add it in ur xml like this

        <xxx.xxxx.utils.CustomTextView 
            android:id="@+id/login_username_tv"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.3"
            android:text="Email"
            android:padding="5dp"
            android:textColor="#333333" 
            android:textSize="12dp"/>
    
    0 讨论(0)
  • 2020-12-11 05:05

    This worked for me in kotlin:

    textView.typeface = ResourcesCompat.getFont(this, R.font.opensans_light)
    
    0 讨论(0)
提交回复
热议问题