对早期设备使用Roboto字体

Bla*_*her 20 android android-layout android-fonts

我想在我的Android应用程序中使用Roboto字体,并确保它适用于没有安装字体的早期版本的Android.我知道我可以通过使用Typeface.createFromAsset()然后手动设置每个TextViews/Buttons/Other-Objects的字体来完成此操作.对于我在屏幕上显示的每个对象来说,这似乎是一个巨大的痛苦.

我的问题是,有更好的方法吗?一些帮助类或在.xml主题文件中设置自定义字体的方法?任何自动化都比手动列出每个屏幕上的每个对象并更改字体更好.

谢谢!

Arn*_*aud 30

以上接受的答案是正确的,但我只是想在这里提供我的实现

我的实用程序类:

package com.example.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AndroidUtils
{
    private static Typeface robotoTypeFace;

    public static void setRobotoFont (Context context, View view)
    {
        if (robotoTypeFace == null)
        {
            robotoTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
        }
        setFont(view, robotoTypeFace);
    }

    private static void setFont (View view, Typeface robotoTypeFace)
    {
        if (view instanceof ViewGroup)
        {
            for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++)
            {
                setFont(((ViewGroup)view).getChildAt(i), robotoTypeFace);
            }
        }
        else if (view instanceof TextView)
        {
            ((TextView) view).setTypeface(robotoTypeFace);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用它,假设this是一个活动:

AndroidUtils.setRobotoFont(this, view);
Run Code Online (Sandbox Code Playgroud)

要为所有TextView设置相同的字体,您可以使用活动的decorView:

ViewGroup godfatherView = (ViewGroup)this.getWindow().getDecorView();
AndroidUtils.setRobotoFont(this, godfatherView);
Run Code Online (Sandbox Code Playgroud)

如果您有适配器或片段,请不要忘记设置其字体.

在这里也看到.


Paw*_*ari 16

检索活动内的所有视图,检查其类型并应用适当的操作.

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
for (View view : allViews)
{
 if (view instanceof TextView) 
 {
    TextView textView = (TextView) view;
    textView.setTypeface(typeface);
  }
}
Run Code Online (Sandbox Code Playgroud)


Fel*_*ipe 7

感谢@Jitsu,@ Arnaud和@Pawan M,我提出了我的解决方案,比单独使用它们更好:

/**
 * Adapted from http://stackoverflow.com/a/12387343/450148
 *
 * @author Anton Averin
 * @author Felipe Micaroni Lalli
 */

package net.alouw.alouwCheckin.util;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.EnumMap;
import java.util.Map;

public final class FontUtils {
    private FontUtils() {
    }

    private enum FontType {
        BOLD("fonts/Roboto/Roboto-BoldCondensed.ttf"),
        BOLD_ITALIC("fonts/Roboto/Roboto-BoldCondensedItalic.ttf"),
        NORMAL("fonts/Roboto/Roboto-Condensed.ttf"),
        ITALIC("fonts/Roboto/Roboto-CondensedItalic.ttf");

        private final String path;

        FontType(String path) {
            this.path = path;
        }

        public String getPath() {
            return path;
        }
    }

    /* cache for loaded Roboto typefaces*/
    private static Map<FontType, Typeface> typefaceCache = new EnumMap<FontType, Typeface>(FontType.class);

    /**
     * Creates Roboto typeface and puts it into cache
     */
    private static Typeface getRobotoTypeface(Context context, FontType fontType) {
        String fontPath = fontType.getPath();

        if (!typefaceCache.containsKey(fontType)) {
            typefaceCache.put(fontType, Typeface.createFromAsset(context.getAssets(), fontPath));
        }

        return typefaceCache.get(fontType);
    }

    /**
     * Gets roboto typeface according to passed typeface style settings.
     * <p/>
     * Will get Roboto-Bold for Typeface.BOLD etc
     */
    private static Typeface getRobotoTypeface(Context context, Typeface originalTypeface) {
        FontType robotoFontType = null;

        if (originalTypeface == null) {
            robotoFontType = FontType.NORMAL;
        } else {
            int style = originalTypeface.getStyle();

            switch (style) {
                case Typeface.BOLD:
                    robotoFontType = FontType.BOLD;
                    break;

                case Typeface.BOLD_ITALIC:
                    robotoFontType = FontType.BOLD_ITALIC;
                    break;

                case Typeface.ITALIC:
                    robotoFontType = FontType.ITALIC;
                    break;

                case Typeface.NORMAL:
                    robotoFontType = FontType.NORMAL;
                    break;
            }
        }

        return (robotoFontType == null) ? originalTypeface : getRobotoTypeface(context, robotoFontType);
    }

    /**
     * Walks ViewGroups, finds TextViews and applies Typefaces taking styling in consideration
     *
     * @param context - to reach assets
     * @param view    - root view to apply typeface to
     */
    public static void setRobotoFont(Context context, View view) {
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                setRobotoFont(context, ((ViewGroup) view).getChildAt(i));
            }
        } else if (view instanceof TextView) {
            Typeface currentTypeface = ((TextView) view).getTypeface();
            ((TextView) view).setTypeface(getRobotoTypeface(context, currentTypeface));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你的onCreate主要活动的最后一件事:

if (Build.VERSION.SDK_INT < 11) {
    ViewGroup godfatherView = (ViewGroup) this.getWindow().getDecorView();
    FontUtils.setRobotoFont(this, godfatherView);
}
Run Code Online (Sandbox Code Playgroud)

在我的自定义视图列表中,上面的代码不起作用,所以我必须这样做:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // (...)

    View view = // build your custom view here

    if (Build.VERSION.SDK_INT < 11) {
        FontUtils.setRobotoFont(activity, view);
    }

    return view;
}
Run Code Online (Sandbox Code Playgroud)