【发布时间】:2011-06-15 02:28:54
【问题描述】:
如何更改 Android TextView 中的 text/font 设置?
例如,如何使文本粗体?
【问题讨论】:
-
请查看我的answer here。希望对你有帮助
如何更改 Android TextView 中的 text/font 设置?
例如,如何使文本粗体?
【问题讨论】:
在layout.xml 文件中执行此操作:
android:textStyle
例子:
android:textStyle="bold|italic"
编程方法是:
setTypeface(Typeface tf)
设置显示文本的字体和样式。请注意,并非所有Typeface 系列实际上都有粗体和斜体变体,因此您可能需要使用setTypeface(Typeface, int) 来获得您真正想要的外观。
【讨论】:
解决办法
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
sans、serif & monospace via xml 只能使用特定字体,Java code 可以使用自定义字体
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)
【讨论】:
如果您使用自定义字体,但您可以使用的字体没有粗体:
myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");
【讨论】:
您可以在 XML 中将 textStyle 设置为 bold,如下所示
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold text"
android:textStyle="bold"/>
您可以如下以编程方式将 TextView 设置为粗体
textview.setTypeface(Typeface.DEFAULT_BOLD);
【讨论】:
设置属性
android:textStyle="bold"
【讨论】:
很简单
setTypeface(Typeface.DEFAULT_BOLD);
【讨论】:
如果你正在绘制它,那么它会做到这一点:
TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
【讨论】:
在理想情况下,您可以像这样在布局 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,但还有更多预定义)。一旦我们有了一个字体实例,我们就可以在 TextView 上设置它。就是这样,我们的内容将以我们定义的样式显示。
希望对你有很大帮助。如需了解更多信息,请访问
http://developer.android.com/reference/android/graphics/Typeface.html
【讨论】:
在 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"
【讨论】:
通过 XML:
android:textStyle="bold"
通过 Java:
//Let's say you have a textview
textview.setTypeface(null, Typeface.BOLD);
【讨论】:
最好的方法是:
TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);
【讨论】:
在文件.xml中,设置
android:textStyle="bold"
将文本类型设置为粗体。
【讨论】:
4 ways to make Android TextView bold- 完整答案在这里。
使用 android:textStyle 属性
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTVIEW 1"
android:textStyle="bold"
/>
使用粗体|斜体表示粗体和斜体。
使用 setTypeface() 方法
textview2.setTypeface(null, Typeface.BOLD);
textview2.setText("TEXTVIEW 2");
HtmlCompat.fromHtml() 方法,Html.fromHtml() 在 API 级别 24 中已弃用。
String html="This is <b>TEXTVIEW 3</b>";
textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));
【讨论】:
假设您是 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
【讨论】:
你可以用这个字体
创建一个类名 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();
}
}
将字体粘贴到资产文件夹中创建的字体文件夹中
<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"
【讨论】:
在我的例子中,通过 string.xml 传递值使用 html Tag..
<string name="your_string_tag"> <b> your_text </b></string>
【讨论】:
editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);
将字体和样式都设置为粗体。
【讨论】:
在 Kotlin 中我们可以一行完成
TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
【讨论】:
你可以这样做
ty.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/magistral.ttf"), Typeface.BOLD);
【讨论】:
textView.setPaintFlags(textView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG)
要删除,请使用
textView.setPaintFlags(textView.getPaintFlags() & ~Paint.FAKE_BOLD_TEXT_FLAG)
或者在 Kotlin 中:
fun TextView.makeBold() {
this.paintFlags = this.paintFlags or Paint.FAKE_BOLD_TEXT_FLAG
}
fun TextView.removeBold() {
this.paintFlags = this.paintFlags and (Paint.FAKE_BOLD_TEXT_FLAG.inv())
}
【讨论】: