Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为应用程序中的所有文本设置fontFamily?_Android - Fatal编程技术网

Android 如何为应用程序中的所有文本设置fontFamily?

Android 如何为应用程序中的所有文本设置fontFamily?,android,Android,我希望应用程序中的每个文本都是无衬线薄的,但我不知道如何更改它。我可以在文本视图中添加android:fontfamine=“sans serif thin”,效果很好,但我不想在每次活动中都更改每个文本视图的代码 我尝试在styles.xml中将sans serif thin添加到AppTheme样式中,但没有效果 如何为应用程序中的所有文本设置fontFamily 解决方案 如果您想使用android默认字体(sans、serif、mono) 试试这个 <style name="App

我希望应用程序中的每个文本都是
无衬线薄的
,但我不知道如何更改它。我可以在
文本视图中添加
android:fontfamine=“sans serif thin”
,效果很好,但我不想在每次活动中都更改每个文本视图的代码

我尝试在
styles.xml
中将
sans serif thin
添加到
AppTheme
样式中,但没有效果

如何为应用程序中的所有文本设置fontFamily

解决方案

如果您想使用android默认字体(sans、serif、mono)

试试这个

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-thin</item>
</style>

@样式/MyTextViewStyle
无衬线薄
附加的


如果要指定自定义字体作为默认字体,可以使用库。此库提供与字体相关的各种功能。

如果您需要为android应用程序中的所有文本视图设置一种字体,则可以使用此解决方案。它将覆盖所有TextView的字体,包括操作栏和其他标准组件,但EditText的密码字体不会被覆盖

public class MyApp extends Application {

@Override
 public void onCreate() {
   TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf
 }
}
别忘了在清单文件的应用程序标记中添加安卓:name=“com.you.yourapp.MyApp”

style.xml中的外接程序主题

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
   <!-- you should set typeface which you want to override with TypefaceUtil -->
   <item name="android:typeface">serif</item>
  </style>
</resources>

这个主题被应用了吗?您可以通过将
android:theme=“@style/AppTheme”
添加到Manifest.xml中的
字段,将主题应用于整个应用程序。是的,这是有效的。4分钟后我才能接受答案。这真是一个愚蠢的规定,现在我不得不呆上4分钟而不是完成工作。
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.lang.reflect.Field;

public class TypefaceUtil {

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE    WHICH WILL BE OVERRIDDEN
 * @param context to work with assets
 * @param defaultFontNameToOverride for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
 public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
    } catch (Exception e) {
        Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
    }
 }
}