简体   繁体   English

在Android中使用自定义字体

[英]Using custom fonts in Android

I'm using abeakrg.TTF font. 我正在使用abeakrg.TTF字体。 As in other tutorials i put it in assets/fonts 与其他教程一样,我将其放在素材资源/字体中

I used this code 我用了这段代码

public class Typefaces {
    private static final String TAG = "Typefaces";

    private final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface tf = Typeface.createFromAsset(getAssets(),
                            "fonts/abeakrg.TTF");

                    cache.put(assetPath, tf);

                    TextView tv3 = (TextView) findViewById(R.id.txt1);
                    tv3.setTypeface(tf);


                } catch (Exception e) {
                    Log.e(TAG, "Could not get typeface '" + assetPath
                            + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }

    }
}

But when i run it in the emulator or the device the font wont appear. 但是,当我在模拟器或设备中运行它时,字体不会出现。 so can i get know the mistake i have done and how to resolve this problem. 这样我就能知道我做的错误以及如何解决此问题。

you can create a subclass from Textview class and settypeface as follow and use in xml 您可以按照以下方法从Textview类和settypeface创建一个子类,并在xml中使用

public class MyTextView extends TextView {

    public MyTextView (Context context) {
        super(context);
        if (!isInEditMode())
            init(context);
    }

    private void init(Context context) {
        setTypeface(Utils.getTypeface(context));
    }

    public MyTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode())
            init(context);
    }

    public MyTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode())
            init(context);
    }

}




public class Utils {
    private static Typeface tf;
public static Typeface getTypeface(Context ctx) {
        if (tf == null) {
            tf = Typeface.createFromAsset(ctx.getAssets(),
                    "fonts/HelveticaNeue.ttf");
        }
        return tf;
    }

    public static Typeface getDolphinTypeface(Context ctx) {
        return Typeface.createFromAsset(ctx.getAssets(), "fonts/GOTHIC.TTF");
    }
}

You can do it by TypeFace 您可以通过TypeFace

for example: if i have xyz_font.ttf style then i will set it like this. 例如:如果我有xyz_font.ttf样式,那么我将这样设置。

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

You need put your xyz_font.ttf file in assets/fonts folder not in res folder. 您需要将xyz_font.ttf文件放置在asset / fonts文件夹中,而不是res文件夹中。

see this custom TextView class 看到这个自定义的TextView

Step 1: create one class CustomTextView 步骤1:创建一个类CustomTextView

public class CustomTextView extends TextView {

static HashMap<Integer, Typeface> typefaces = new HashMap<Integer, Typeface>(
        5);

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomTextView);

    String typefacename = a.getString(R.styleable.CustomTextView_typeface);

    if (typefacename != null) {
        try {

            Typeface typeface = typefaces.get(typefacename.hashCode());

            if (typeface == null) {
                typeface = Typeface.createFromAsset(context.getAssets(),
                        typefacename);
                typefaces.put(typefacename.hashCode(), typeface);
            }

            this.setTypeface(typeface);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 public CustomTextView(Context context) {
    super(context);
 }

}

Step 2: 第2步:

create attrs.xml in value folder 在值文件夹中创建attrs.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
 <declare-styleable name="CustomTextView">
   <attr name="typeface" format="string" />
 </declare-styleable>
</resources>

step 3 Copy required fonts into the assets folder of your project 步骤3将所需的字体复制到项目的资产文件夹中

step 4: Instead of TextView, use CustomTextView.java in your layout (you can find your customTextView in XML layout editor in custom view section ) 步骤4:在布局中使用CustomTextView.java而不是TextView(您可以在XML布局编辑器的“定制视图”部分中找到customTextView)

use custom TextView like this 使用这样的自定义TextView

  <com.your.path.CustomTextView
    android:id="@+id/custom_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="custom font"
    app:typeface="your_font.ttf" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM