android 设置文本加粗,如何在Android中将文本更改为粗体?

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

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

15个解决方案

482 votes

要在Typeface文件中执行此操作:

android:textStyle

例子:

android:textStyle="bold|italic"

以编程方式,该方法是:

setTypeface(Typeface tf)

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

Phobos answered 2019-03-31T19:17:50Z

327 votes

这是解决方案

TextView questionValue = (TextView) findViewById(R.layout.TextView01);

questionValue.setTypeface(null, Typeface.BOLD);

Sudipta Som answered 2019-03-31T19:18:10Z

62 votes

您只需执行以下操作:

在XML中设置属性

android:textStyle="bold"

以编程方式,该方法是:

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

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

Tv.setTypeface(boldTypeface);

希望这有助于你感谢你。

saeed answered 2019-03-31T19:18:58Z

50 votes

在XML中

android:textStyle="bold" //only bold

android:textStyle="italic" //only italic

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

您只能使用特定字体sans,serif& monospace通过xml,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)

Salim answered 2019-03-31T19:19:36Z

21 votes

设置属性

android:textStyle="bold"

koljaTM answered 2019-03-31T19:20:01Z

15 votes

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

TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);

noelicus answered 2019-03-31T19:20:26Z

15 votes

这很容易

setTypeface(Typeface.DEFAULT_BOLD);

HatemTmi answered 2019-03-31T19:20:50Z

14 votes

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

myTextView.setText(Html.fromHtml("" + myText + "");

Niko answered 2019-03-31T19:21:16Z

14 votes

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

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,但还有更多预定义的)。 一旦我们有了一个字体实例,我们就可以在TextView上设置它。 这就是我们的内容将显示在我们定义的样式上。

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

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

IntelliJ Amiya answered 2019-03-31T19:22:08Z

9 votes

在values文件夹的style.xml文件中使用所需的格式定义新样式

bold

monospace

16sp

#5EADED

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

style="@style/TextViewStyle"

Seven answered 2019-03-31T19:22:47Z

5 votes

最好的方法是:

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

tv.setTypeface(Typeface.DEFAULT_BOLD);

sabbibJAVA answered 2019-03-31T19:23:16Z

4 votes

假设您是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

Dan Tilakaratne answered 2019-03-31T19:23:42Z

3 votes

您可以将其用于字体

创建一个类名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();

}

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();

}

}

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

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"

Amaresh Jana answered 2019-03-31T19:24:33Z

1 votes

在我的例子中,通过string.xml传递值与html标签一起解决..

your_text

Rajesh Naddy answered 2019-03-31T19:25:05Z

0 votes

editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));

etitText.setTypeface(et.getTypeface(), Typeface.BOLD);

将Bold设置为字体和样式。

Sanket Patankar answered 2019-03-31T19:25:31Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值