How do you change text to bold in Android?

后端 未结 19 734
走了就别回头了
走了就别回头了 2020-12-02 04:21

How do you change text/font settings in an Android TextView?

For example, how do you make the text bold

19条回答
  •  臣服心动
    2020-12-02 05:21

    You can use this for font

    create a Class Name TypefaceTextView and extend the TextView

    private static Map mTypefaces;

    public TypefaceTextView(final Context context) {
        this(context, null);
    }
    
    public TypefaceTextView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        if (mTypefaces == null) {
            mTypefaces = new HashMap();
        }
    
        if (this.isInEditMode()) {
            return;
        }
    
        final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView);
        if (array != null) {
            final String typefaceAssetPath = array.getString(
                    R.styleable.TypefaceTextView_customTypeface);
    
            if (typefaceAssetPath != null) {
                Typeface typeface = null;
    
                if (mTypefaces.containsKey(typefaceAssetPath)) {
                    typeface = mTypefaces.get(typefaceAssetPath);
                } else {
                    AssetManager assets = context.getAssets();
                    typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                    mTypefaces.put(typefaceAssetPath, typeface);
                }
    
                setTypeface(typeface);
            }
            array.recycle();
        }
    }
    

    paste the font in the fonts folder created in the asset folder

    **here pompiere.ttf is the font name**
    

    Place the lines in the parent layout in the xml

     xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    

提交回复
热议问题