如何在Android中将文本更改为粗体?

nit*_*hin 403 android text textview

如何更改Android中的文本/字体设置TextView?例如,如何使文本变粗

小智 521

要在layout.xml文件中执行此操作:

android:textStyle
Run Code Online (Sandbox Code Playgroud)

例子:

android:textStyle="bold|italic"
Run Code Online (Sandbox Code Playgroud)

以编程方式,该方法是:

setTypeface(Typeface tf)
Run Code Online (Sandbox Code Playgroud)

设置应显示文本的字体和样式.请注意,并非所有Typeface系列都具有粗体和斜体变体,因此您可能需要使用它setTypeface(Typeface, int)来获得您真正想要的外观.


Sud*_*Som 345

这是解决方案

TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)


sae*_*eed 72

您只需执行以下操作:

设置属性 XML

  android:textStyle="bold"
Run Code Online (Sandbox Code Playgroud)

以编程方式,该方法是:

TextView Tv = (TextView) findViewById(R.id.TextView);

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);

Tv.setTypeface(boldTypeface);
Run Code Online (Sandbox Code Playgroud)

希望这有助于你感谢你.


Sal*_*lim 53

在XML中

android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic
Run Code Online (Sandbox Code Playgroud)

您只能使用特定的字体sans,serifmonospace通过XML,Java代码可以使用自定义字体

android:typeface="monospace" // or sans or serif
Run Code Online (Sandbox Code Playgroud)

以编程方式(Java代码)

TextView textView = (TextView) findViewById(R.id.TextView1);

textView.setTypeface(Typeface.SANS_SERIF); //only font style
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD); 
                                         //font style & text style(only bold)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
                                         //font style & text style(bold & italic)
Run Code Online (Sandbox Code Playgroud)


小智 23

从 XML 中,您可以将textStyle设置为粗体,如下所示

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Bold text"
   android:textStyle="bold"/>
Run Code Online (Sandbox Code Playgroud)

您可以如下以编程方式将 TextView 设置为粗体

textview.setTypeface(Typeface.DEFAULT_BOLD);
Run Code Online (Sandbox Code Playgroud)


kol*_*aTM 22

设置属性

android:textStyle="bold"
Run Code Online (Sandbox Code Playgroud)


Nik*_*iko 18

对于使用自定义字体但不使用粗体字体的情况,您可以使用:

myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");
Run Code Online (Sandbox Code Playgroud)


Hat*_*Tmi 17

这很容易

setTypeface(Typeface.DEFAULT_BOLD);
Run Code Online (Sandbox Code Playgroud)


noe*_*cus 16

如果您正在绘制它,那么这样做:

TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)


Int*_*iya 15

在理想世界中,您可以在布局XML定义中设置文本样式属性,如下所示:

<TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"/>
Run Code Online (Sandbox Code Playgroud)

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

TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);
Run Code Online (Sandbox Code Playgroud)

第一行将创建预定义样式的对象形式(在本例中为Typeface.BOLD,但还有更多预定义的).一旦我们有了一个字体实例,我们就可以在TextView上设置它.这就是我们的内容将显示在我们定义的样式上.

我希望它对你有很大的帮助.有关更好的信息,你可以访问

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


小智 10

在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>
Run Code Online (Sandbox Code Playgroud)

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

style="@style/TextViewStyle"
Run Code Online (Sandbox Code Playgroud)


sab*_*AVA 8

最好的方法是:

TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);
Run Code Online (Sandbox Code Playgroud)


Ath*_*ddy 7

使 Android TextView 加粗的 4 种方法- 完整答案在这里。

  1. 使用android:textStyle属性

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEXTVIEW 1" android:textStyle="bold" /> 使用粗体|斜体表示粗体和斜体。

  2. 使用 setTypeface() 方法

    textview2.setTypeface(null, Typeface.BOLD);
    textview2.setText("TEXTVIEW 2");
    
    Run Code Online (Sandbox Code Playgroud)
  3. HtmlCompat.fromHtml() 方法,Html.fromHtml() 在 API 级别 24 中已弃用。

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


小智 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
Run Code Online (Sandbox Code Playgroud)


小智 6

通过 XML:

 android:textStyle="bold"
Run Code Online (Sandbox Code Playgroud)

通过Java:

//Let's say you have a textview 
textview.setTypeface(null, Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)


Ama*_*ana 5

您可以使用此字体

创建一个类名称TypefaceTextView并扩展TextView

私有静态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();
    }
}
Run Code Online (Sandbox Code Playgroud)

将字体粘贴到资产文件夹中创建的fonts文件夹中

<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**
Run Code Online (Sandbox Code Playgroud)

将这些行放在xml的父级布局中

 xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf"
xmlns:custom="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)


Raj*_*ddy 5

在我的情况下,通过 string.xml 传递值使用 html 标签解决了..

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


小智 5

文件.xml中,设置

android:textStyle="bold" 
Run Code Online (Sandbox Code Playgroud)

将文本类型设置为粗体。