Android 如何将文本视图的字体更改为sans、serif或monospace?

Android 如何将文本视图的字体更改为sans、serif或monospace?,android,Android,我不想自定义字体。我只想使用android studio中提供的字体。如何将文本的字体更改为sans、serif或monospace?-Hello 照此 -android:typeface在xml中用于更改字体 -您的最终代码是:- <Textview .......... =android:typeface="normal/sans/monospace"..../> 首先是所需字体的.ttf文件(arial.ttf)。将其放置在assets文件夹中(在assest文件夹中创建名

我不想自定义字体。我只想使用android studio中提供的字体。如何将文本的字体更改为sans、serif或monospace?

-Hello 照此

-
android:typeface
在xml中用于更改字体

-您的最终代码是:-

<Textview .......... =android:typeface="normal/sans/monospace"..../>

首先是所需字体的.ttf文件(arial.ttf)。将其放置在assets文件夹中(在assest文件夹中创建名为“fonts”的新文件夹并将其放置在其中)。如果“txtyour”是要应用字体的文本,请使用以下代码

 Typeface type = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf"); 
 txtview.setTypeface(type);

如果你想改变整个应用程序的字体,那么我建议你使用这个样式

只需将以下行添加到styles.xml文件中即可-

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/TextAppearance.Sans</item>
    <item name="android:buttonStyle">@style/TextAppearance.Sans</item>
</style>

<style name="TextAppearance.Sans" parent="TextAppearance.AppCompat.Large">
        <item name="android:fontFamily">sans-serif-light</item>
        <item name="android:textStyle">normal</item>
</style>

@style/TextAppearance.Sans
@style/TextAppearance.Sans
无衬线灯
正常的
如果您不想在整个应用程序中更改字体,则只需在每个要更改字体的TextView中定义样式元素,而不是在AppTheme中进行设置-

<TextView
    style="@style/TextAppearance.Sans"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

看看这个。