如何在Android中将文本加粗?

515

如何更改 Android TextView 中的文本/字体设置?

例如,如何使文本加粗?


请查看我在此处的答案:(http://stackoverflow.com/a/40802895/1252158)。 希望这能帮到你。 - Summved Jain
3
请点击此处阅读有关如何在Android TextView中设置粗体字的文章。 - Thomas Daniel
22个回答

16

在理想的情况下,您可以像这样在布局XML定义中设置文本样式属性:

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

通过使用 setTypeface 方法,您可以在代码中以动态方式轻松实现相同的结果。您需要传递 Typeface 类的对象,该对象将描述 TextView 的字体样式。因此,要实现与上述 XML 定义相同的结果,您可以执行以下操作:

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

第一行将创建一个对象,使用预定义的样式(在本例中为 Typeface.BOLD,但还有许多其他预定义的样式)。一旦我们获得了 Typeface 的实例,就可以在 TextView 上设置它。这就是我们定义的样式显示内容的全部过程。

希望这能对您有所帮助。如需更好的信息,您可以访问:

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


11

在values文件夹中的style.xml文件中定义一个新样式,其格式按照您所需的方式进行。

<style name="TextViewStyle" parent="AppBaseTheme">
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#5EADED</item>

</style>

然后,通过编写以下代码以使用TextView的属性将此样式应用于TextView

style="@style/TextViewStyle"

10
文件 .xml中,设置。
android:textStyle="bold" 

将设置文本类型为粗体。


9
最好的方法是:
TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);

8

4种让Android TextView加粗的方法 - 完整答案在此处。

  1. Using android:textStyle attribute

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEXTVIEW 1" android:textStyle="bold" /> Use bold|italic for bold and italic.

  2. using setTypeface() method

    textview2.setTypeface(null, Typeface.BOLD);
    textview2.setText("TEXTVIEW 2");
    
  3. HtmlCompat.fromHtml() method, Html.fromHtml() was deprecated in API level 24.

     String html="This is <b>TEXTVIEW 3</b>";
     textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));
    

6
在我的情况下,通过string.xml传递值可以使用html标签解决。 <string name="your_string_tag"> <b> your_text </b></string>

6
假设你是Android Studio的新手, 你可以在设计视图中使用XML轻松完成。
android:textStyle="bold"          //to make text bold
android:textStyle="italic"        //to make text italic
android:textStyle="bold|italic"   //to make text bold & italic

5
你可以使用这段代码来设置字体。
创建一个 Class Name 为 TypefaceTextView 并继承 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();
    }
}

把字体粘贴到在资产文件夹中创建的字体文件夹中。
<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**

在xml的父布局中放置这些行。
 xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf"
xmlns:custom="http://schemas.android.com/apk/res-auto"

3
editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);

将字体和样式都设置为粗体。


2
你应该包含一个简短的解释来更好地描述它的使用情况和实现。 - Dov Benyomin Sohacheski
这并没有为现有的答案添加任何新的内容。 - nvoigt
@nvoigt,他的答案与许多其他答案不同,至少第二行是这样。可能比其他解决方案更好。我认为它来自https://dev59.com/l2025IYBdhLWcg3wIyKf#42484530。 - CoolMind

2
在 Kotlin 中,我们可以用一行代码实现。
 TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接