我有三个文本视图。我需要将它们的字体设置为Light、Regular和Condensed。请问有人能帮我在Android中实现这个功能吗?
我有三个文本视图。我需要将它们的字体设置为Light、Regular和Condensed。请问有人能帮我在Android中实现这个功能吗?
使用android:textStyle
在TextView
上设置文本样式,如粗体,斜体或普通。
以下是带有粗体文本样式的TextView
示例:
<TextView
android:id="@+id/hello_world"
android:text="hello world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
如果您想使用轻或紧缩字体,您需要更改字体。您可以这样做:
android:fontFamily="sans-serif-light"
有关字体的更多信息,请参阅以下答案android:fontFamily的有效值及其映射?
private TextView tv;
然后在onCreate()中,引用你保存在/assets文件夹中的字体文件:
Typeface someFontName-condensed = Typeface.createFromAsset(getAssets(), "assets/NameOfFont-condensed.ttf");
tv = (TextView) findViewById(R.id.myTextViewId);
tv.setTypeface(someFontName-condensed);