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

如何在Android TextView更改文本/字体设置? 例如,你如何使文字加粗

要在layout.xml文件中做到这一点:

 android:textStyle 

例子:

 android:textStyle="bold|italic" 

编程方式是:

 setTypeface(Typeface tf) 

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

这是解决scheme

 TextView questionValue = (TextView) findViewById(R.layout.TextView01); questionValue.setTypeface(null, Typeface.BOLD); 

只要你可以做到以下几点:

XML设置属性

  android:textStyle="bold" 

编程方法是:

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

希望这可以帮助你,谢谢你。

在XML中

 android:textStyle="bold" //only bold android:textStyle="italic" //only italic android:textStyle="bold|italic" //bold & italic 

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

 android:typeface="monospace" // or sans or serif 

以编程方式(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) 

设置属性

 android:textStyle="bold" 

如果你正在绘制它,那么这将做到这一点:

 TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG); 

这很容易

 setTypeface(Typeface.DEFAULT_BOLD); 

在理想的世界里,你可以在你的布局XML定义中设置文本样式属性:

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

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

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

第一行将创build对象窗体预定义的样式(在这种情况下, Typeface.BOLD ,但还有更多的预定义)。 一旦我们有一个字体的实例,我们可以在TextView上设置它。 这就是我们的内容将以我们定义的风格显示。

我希望它可以帮助你很多。为了更好的信息,你可以访问

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

如果您使用的是自定义字体,但是没有粗体字体,您可以使用:

 myTextView.setText(Html.fromHtml("<b>" + myText + "</b>"); 

在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" 

你可以使用这个字体

创build一个类名字TypeTextView并扩展TextView

私人静态地图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(); } } 

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

 <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" 

最好的方法是:

 TextView tv = findViewById(R.id.textView); tv.setTypeface(Typeface.DEFAULT_BOLD); 

假设您是Android Studio的新手,只需使用devise视图XML即可完成

 android:textStyle="bold" //to make text bold android:textStyle="italic" //to make text italic android:textStyle="bold|italic" //to make text bold & italic