How do you change text to bold in Android?

后端 未结 19 619
走了就别回头了
走了就别回头了 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:17

    In my case, Passing value through string.xml worked out with html Tag..

    <string name="your_string_tag"> <b> your_text </b></string>

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

    In Kotlin we can do in one line

     TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
    
    0 讨论(0)
  • 2020-12-02 05:21

    In the ideal world you would set the text style attribute in you layout XML definition like that:

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"/>
    

    There is a simple way to achieve the same result dynamically in your code by using setTypeface method. You need to pass and object of Typeface class, which will describe the font style for that TextView. So to achieve the same result as with the XML definition above you can do the following:

    TextView Tv = (TextView) findViewById(R.id.TextView);
    Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
    Tv.setTypeface(boldTypeface);
    

    The first line will create the object form predefined style (in this case Typeface.BOLD, but there are many more predefined). Once we have an instance of typeface we can set it on the TextView. And that's it our content will be displayed on the style we defined.

    I hope it helps you a lot.For better info you can visit

    http://developer.android.com/reference/android/graphics/Typeface.html

    0 讨论(0)
  • 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<String, Typeface>();
        }
    
        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

    <packagename.TypefaceTextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:gravity="center"
            android:text="TRENDING TURFS"
            android:textColor="#000"
            android:textSize="20sp"
            app:customTypeface="fonts/pompiere.ttf" />**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"
    
    0 讨论(0)
  • 2020-12-02 05:22

    It's very easy

    setTypeface(Typeface.DEFAULT_BOLD);
    
    0 讨论(0)
  • 2020-12-02 05:24

    Here is the solution

    TextView questionValue = (TextView) findViewById(R.layout.TextView01);
    questionValue.setTypeface(null, Typeface.BOLD);
    
    0 讨论(0)
提交回复
热议问题