【问题标题】:Adding custom Font to Theme in Android在 Android 中向主题添加自定义字体
【发布时间】:2012-02-05 22:40:27
【问题描述】:

有没有办法在 Android 的 Themes 中添加自定义字体?

我已阅读 Quick Tip: Customize Android Fonts,但在这里我们必须以编程方式将自定义字体添加到文本中。

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font); 

但我想按样式/主题设置自定义字体。

【问题讨论】:

    标签: android fonts textview


    【解决方案1】:

    我希望这是您的意思,但如果不是,无论如何它应该是对其他人的一个很好的参考。

    ** 注意:字体可以在计算机/本地磁盘 (C:)/Windows/Fonts 中找到 **

    在上面的 Fonts 文件夹中复制您要使用的字体,并将其粘贴到 Eclipse 中 assets 文件夹中新创建的文件夹中。

        private void initTypeFace()
        {
            TypeFace tf = TypeFace.createFromAsset(getAsset(),
                          "Chantelli Antiqua.ttf");
    
            TextView txt = (TextView) findViewById(R.id.custom_font);
    
            txt.setTypeface(tf);
            example_button1.setTypeface(tf);
            example_button2.setTypeface(tf);
        }
    

    【讨论】:

      【解决方案2】:

      我认为您的问题的答案将是带有附加 XML 参数的自定义 TextView,您可以将其包含到您的主题中。

      你可以在构造函数TextView(Context context, AttributeSet attrs)中解析这个值来初始化它。检查this 链接,例如为您的视图定义自定义属性并初始化它们。

      【讨论】:

        【解决方案3】:

        我认为这是 this question 和 this one 的重复。

        在我的运行时活动中,我使用这样的东西:

        FontUtils.setCustomFont(findViewById(R.id.top_view), getAssets());
        

        在 XML 中:

                <TextView
                    android:id="@+id/my_label"
                    android:tag="condensed"
                    android:text="@string/label"
                    ... />
        

        所以理论上你可以创建样式并将其与 FontUtils/运行时代码一起使用。

        <style name="roboto_condensed">
            <item name="android:tag">condensed,your-own-css-like-language-here</item>
        </style>
        

        FontUtils 类:

        public class FontUtils {
          private static Typeface normal;
        
          private static Typeface bold;
        
          private static Typeface condensed;
        
          private static Typeface light;
        
          private static void processsViewGroup(ViewGroup v, final int len) {
        
            for (int i = 0; i < len; i++) {
              final View c = v.getChildAt(i);
              if (c instanceof TextView) {
                setCustomFont((TextView) c);
              } else if (c instanceof ViewGroup) {
                setCustomFont((ViewGroup) c);
              }
            }
          }
        
          private static void setCustomFont(TextView c) {
            Object tag = c.getTag();
            if (tag instanceof String) {
              if (((String) tag).contains("bold")) {
                c.setTypeface(bold);
                return;
              }
              if (((String) tag).contains("condensed")) {
                c.setTypeface(condensed);
                return;
              }
              if (((String) tag).contains("light")) {
                c.setTypeface(light);
                return;
              }
            }
            c.setTypeface(normal);
          }
        
          public static void setCustomFont(View topView, AssetManager assetsManager) {
            if (normal == null || bold == null || condensed == null || light == null) {
              normal = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Regular.ttf");
              bold = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Bold.ttf");
              condensed = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Condensed.ttf");
              light = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Light.ttf");
            }
        
            if (topView instanceof ViewGroup) {
              setCustomFont((ViewGroup) topView);
            } else if (topView instanceof TextView) {
              setCustomFont((TextView) topView);
            }
          }
        
          private static void setCustomFont(ViewGroup v) {
            final int len = v.getChildCount();
            processsViewGroup(v, len);
          }
        }
        

        【讨论】:

          【解决方案4】:

          您可以在资产文件夹中包含您的自定义字体类型并从那里检索它。

          将字体声明为:

          Typeface helveticaBold;
          Typeface helveticaRegular;
          

          在 onCreate() 中编写如下代码:

          helveticaBold = Typeface.createFromAsset(getAssets(), "helvetica_bold.ttf");
          helveticaRegular = Typeface.createFromAsset(getAssets(), "helvetica_regular.ttf");
          

          最后,将TextView或EditText的文字字体设置为:

          editText.setTypeface(helveticaRegular);
          

          就是这样……

          【讨论】:

            【解决方案5】:

            通过使用我的 CustomTextView,您可以直接在 XML 布局文件中的 assets 文件夹中指定字体文件名。

            我的答案是here

            【讨论】:

              【解决方案6】:

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

              以下是如何应用这些工具的示例。把你所有的字体文件放在assets/fonts/。然后,在 xml 文件中声明这些字体(例如 res/xml/fonts.xml)并在您的应用程序中尽早使用 TypefaceManager.initialize(this, R.xml.fonts); 加载此文件(例如,在您的 Application 类的 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>
              

              现在您可以在您的样式或 xml 中使用这些字体(前提是您使用我上面提到的工具),方法是在您的 xml 布局中的自定义 TextView com.innovattic.font.FontTextView 中设置 flFont 属性。您可以在下面看到如何将字体应用于整个应用程序中的所有文本,只需编辑res/values/styles.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>
              
                  <!-- Alternative style, maybe for some other widget -->
                  <style name="StylishFont">
                      <item name="flFont">anotherFont</item>
                      <item name="android:textStyle">normal</item>
                  </style>
              
              </resources>
              

              附随res/layout/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" >
              
                  <!-- This text view is styled with the app theme -->
                  <com.innovattic.font.FontTextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="This uses my font in bold italic style" />
              
                  <!-- This text view is styled here and overrides the app theme -->
                  <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" />
              
                  <!-- This text view is styled with a style and overrides the app theme -->
                  <com.innovattic.font.FontTextView
                      style="@style/StylishFont"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="This also uses another font in normal style" />
              
              </LinearLayout>
              

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

              【讨论】:

              • 既然你也在使用java代码,那么我们可以用更简单的方式来做
              • 这真的应该是android原生的
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-01-09
              • 1970-01-01
              • 2014-05-23
              • 1970-01-01
              • 1970-01-01
              • 2016-02-18
              • 2013-07-14
              相关资源
              最近更新 更多