问题描述:

I want to make an app in a non English language. So I need the textviews, strings and the toast to be in the non-English language. Setting the typeface for every text is cumbersome. Is there a way to set a default font? When I have to refer to something in English (like email id) then I can use the textview.settypeface(xyz) method.

网友答案:

There is a grate library for custom fonts in android: custom fonts

Here is a sample how to use it.

In gradle you need to put this line:

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

and then make a class that extends application an write this code:

public class App extends Application { 
@Override public void onCreate() { super.onCreate();

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("your font path")
                .setFontAttrId(R.attr.fontPath)
                .build()
);
}

}

and in the activity class put this method before onCreate:

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));

and the last thing in your manifest file write like this:

<application
android:name=".App"

and it will change the whole activity to your font!. I'ts simple and clean!

网友答案:

You can achieve this in mainly 3 ways. one way would be to create a custom TextView and refer that everywhere, ie :

    public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}

and inside View.xml

<packagename.TypefacedTextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Hello"/>

Another way, is to use the powerful Calligraphy library in github. see this

And finally, you can override the defaults with your own fonts, see this

相关阅读:
Top