【问题标题】:change font of all textviews更改所有文本视图的字体
【发布时间】:2016-05-20 06:30:43
【问题描述】:

有没有办法改变布局中所有文本视图的字体?

目前我正在使用它来手动更改字体。

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

Typeface tf = Typeface.createFromAsset(getAssets(),
    "fonts/Existence-Light.otf");
txtAppName.setTypeface(tf);

编辑:我已编辑问题以澄清我的问题。

【问题讨论】:

  • 据我所知,它“只是”一个带有 id="txtAppName" 的 TextView - o.O 没有任何问题
  • 那么,您的意思只是如何更改一个应用程序的所有视图的默认字体?

标签: android


【解决方案1】:

对您来说可能有点晚了,但对于其他 Android 用户来说,这可能非常有用。

很遗憾,Android 没有提供您想要的快速、简单和干净的方式来更改整个应用的字体。但最近我研究了这个问题,并创建了一些工具,让您无需任何编码即可更改字体(您可以通过 xml、样式甚至文本外观来完成这一切)。您可以在 this blog 上阅读所有相关信息,并查看 github 项目 here

以下是如何应用这些工具的示例。把你所有的字体文件放在assets/fonts/。然后,在 xml 文件中声明这些字体,并在应用程序的早期使用 TypefaceManager.initialize(this, R.xml.fonts); 加载此文件(例如,在应用程序类的 onCreate 中)。 xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<familyset>

    <!-- Some Font. Can be referenced with 'someFont' or 'aspergit' -->
    <family>
        <nameset>
            <name>aspergit</name>
            <name>someFont</name>
        </nameset>
        <fileset>
            <file>Aspergit.ttf</file>
            <file>Aspergit Bold.ttf</file>
            <file>Aspergit Italic.ttf</file>
            <file>Aspergit Bold Italic.ttf</file>
        </fileset>
    </family>

    <!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' -->
    <family>
        <nameset>
            <name>bodoni</name>
            <name>anotherFont</name>
        </nameset>
        <fileset>
            <file>BodoniFLF-Roman.ttf</file>
            <file>BodoniFLF-Bold.ttf</file>
        </fileset>
    </family>

</familyset>

现在您可以通过 flFont 属性在您的样式或 xml 中使用它们。以下是如何将字体应用于整个应用中的所有文本:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

    <!-- Application theme -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    </style>

    <!-- Style to use for ALL text views (including FontTextView) -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView">
        <item name="android:textAppearance">@style/MyTextAppearance</item>
    </style>

    <!-- Text appearance to use for ALL text views (including FontTextView) -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo">
        <!-- Alternatively, reference this font with the name "aspergit" -->
        <!-- Note that only our own TextView's will use the font attribute -->
        <item name="flFont">someFont</item>
        <item name="android:textStyle">bold|italic</item>
    </style>

</resources>

现在你可以让 layout.xml 看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This uses my font in bold italic style" />

    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:flFont="anotherFont"
        android:textStyle="normal"
        android:text="This uses another font in normal style" />

</LinearLayout>

不要忘记在您的 Android 清单中应用主题。

【讨论】:

    【解决方案2】:

    您可以制作自己的视图来设置字体并使用它

    public class MyTextView extends TextView {
         public MyTextView(Context context) {
            super(context, attrs, defStyle);
            setFont(context);
         }
    
         private void setFont(Context context) {
            Typeface font = Typeface.createFromAsset(context.getAssets(), YOUR_FONT);
            setTypeface(font);
         }
    }
    

    【讨论】:

    • 我正在使用此代码有没有办法检查 textStyle = bold 以便我可以将字体交换为 MY_FONT_BOLD?
    • 你可以使用方法 setTypeface(font, MY_FONT_BOLD)
    • 不,我的意思是如果 attr 设置为粗体,我尝试将 attr 值获取到字符串变量但使用 .contains 函数,将导致强制关闭
    • 对不起,我不明白你的意思。
    【解决方案3】:

    用户可以在设置中更改默认字体,它适用于所有应用程序,因此如果您能找到为用户设置此设置的方法,您将在他的所有应用程序中更改他的字体。

    我的建议是为 TextView 创建一个解脱,并在该类中只设置一次字体。

    【讨论】:

    • 我只需要更改我的应用程序的字体,例如将所有文本视图设置为某个 otf 字体,
    • 我还没有看到用户可以更改默认字体的设置。但是,用户可以更改字体大小。
    • 所以你应该听从巴比布的建议,并将 TextView 类扩展到例如自动调用字体设置代码的 MyTextView。
    【解决方案4】:

    如果你有自己的自定义字体,你应该把它添加到assets->fonts文件夹,可以如下创建:

    然后创建字体文件夹并将您的“ttf”文件粘贴到那里。

    然后需要像这样以编程方式遍历所有 TextView 并设置字体(我的是“fonts/Qlassik_TB.ttf”):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Your stuff here...
    
        Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/Qlassik_TB.ttf");
    
        ViewGroup myMostParentLayout = (ViewGroup) findViewById(R.id.frame_layout_most_parent);
    
        setFontToAllChilds(myMostParentLayout, tf);
    }
    
    private void setFontToAllChilds(ViewGroup myMostParentLayout, Typeface tf) {
        int childCount = myMostParentLayout.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            View child = myMostParentLayout.getChildAt(i);
    
            if (child instanceof ViewGroup)
                setFontToAllChilds((ViewGroup) child, tf);
            else if (child instanceof TextView)                
                ((TextView) child).setTypeface(tf);
    }
    

    最后添加一个 id 到你最父的 WhatLayout:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frame_layout_most_parent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    

    这里使用的是:

    ViewGroup myMostParentLayout = (ViewGroup) findViewById(R.id.frame_layout_most_parent);
    

    【讨论】:

      【解决方案5】:

      您可以获取的所有系统默认字体都在:

      android/res/values/styles.xml

      您只能通过设置等系统应用程序更改它们。也许你可以调用一些系统函数来完成它。但这似乎不太可能,因为这会给所有其他应用程序带来问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        相关资源
        最近更新 更多