在Android中为主题添加自定义字体

Gau*_*sth 44 fonts android textview

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

我已阅读快速提示:自定义Android字体,但在这里我们必须以编程方式将自定义字体添加到文本中.

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

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

Jel*_*sen 21

不幸的是,Android并没有提供快速,简单和干净的方式来改变整个应用程序的字体.但是最近我调查了这个问题并创建了一些工具,允许你在没有任何编码的情况下更改字体(你可以通过xml,样式甚至文本外观来完成).它们基于类似于您在其他答案中看到的类似解决方案,但允许更大的灵活性.您可以在此博客上阅读所有相关内容,并在此处查看github项目.

以下是如何应用这些工具的示例.将所有字体文件放入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>
Run Code Online (Sandbox Code Playgroud)

现在,您可以在样式或xml中使用这些字体(前提是您使用我上面提到的工具),方法是com.innovattic.font.FontTextView在xml布局中的自定义TextView 中设置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>
Run Code Online (Sandbox Code Playgroud)

随附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>
Run Code Online (Sandbox Code Playgroud)

不要忘记在Android清单中应用主题.


Mac*_*ski 12

我认为这是这个问题这个问题的重复.

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

FontUtils.setCustomFont(findViewById(R.id.top_view), getAssets());
Run Code Online (Sandbox Code Playgroud)

在XML中:

        <TextView
            android:id="@+id/my_label"
            android:tag="condensed"
            android:text="@string/label"
            ... />
Run Code Online (Sandbox Code Playgroud)

因此,从理论上讲,您可以创建样式并将其与FontUtils /运行时代码一起使用.

<style name="roboto_condensed">
    <item name="android:tag">condensed,your-own-css-like-language-here</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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);
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 -1

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

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

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

    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);
    }
Run Code Online (Sandbox Code Playgroud)