我想在我的应用中设置自定义字体,然后完成 Is it possible to set a custom font for entire of application? 我已经完成了但问题是,当我应用 AppTheme_1 时,它会更改大于或等于棒棒糖的Android版本的字体,当我应用 AppTheme_2 然后它改变仅少于棒棒糖的Android版本的字体。但我想改变所有Android版本的字体。
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="AppTheme_1" parent="AppBaseTheme">
<!-- Customize your theme here. -->
<item name="android:textAppearance">@style/CustomTextAppearance</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="CustomTextAppearance">
<item name="android:typeface">serif</item>
</style>
</style>
<style name="AppTheme_2" parent="AppBaseTheme">
<!-- Customize your theme here. -->
<item name="android:typeface">serif</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowContentOverlay">@null</item>
</style>
答案 0 :(得分:2)
我们可以使用书法库
将单个字体设置为整个应用程序以下是图书馆链接:Calligraphy
现在在build.gradle中添加compile 'uk.co.chrisjenx:calligraphy:2.3.0'
现在创建一个如下所示的应用程序类:
public class MyApplication extends Application {
private static MyApplication sInstance;
public static MyApplication getInstance() {
return sInstance;
}
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Roboto-Regular.ttf")
.setFontAttrId(R.attr.fontPath)
.build());
}}
现在您需要在应用程序标记
中的清单文件中调用此应用程序类 android:name=".MyApplication"
通过这样做,整个应用程序都设置了一种字体。您可以浏览图书馆以便更好地理解。
答案 1 :(得分:0)
试试这个
MyApplication.java(这是Application类)
//Typeface
public static Typeface poppinsSemiBold;
public static Typeface helveticaNeueMedium;
public static Typeface poppinsRegular;
/**
* Preload typefaces.
*/
private void preloadTypefaces() {
poppinsSemiBold = Typefaces.get(getApplicationContext(), Typefaces.FONT_POPPINS_SEMI_BOLD);
helveticaNeueMedium = Typefaces.get(getApplicationContext(), Typefaces.FONT_HELVETICANEUE_MEDIUM);
poppinsRegular = Typefaces.get(getApplicationContext(), Typefaces.FONT_POPPINS_REGULAR);
}
在同一个类MyApplication.java的onCreate()中调用此preloadTypefaces()
Typefaces.java
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.util.Hashtable;
public class Typefaces {
private static final String TAG = Typefaces.class.getSimpleName();
public static final String FONT_POPPINS_SEMI_BOLD = "fonts/Poppins_SemiBold.ttf";
public static final String FONT_HELVETICANEUE_MEDIUM = "fonts/HelveticaNeue_Medium.otf";
public static final String FONT_POPPINS_REGULAR = "fonts/Poppins_Regular.ttf";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(), assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
确保您的.ttf或.otf文件位于assets / fonts / HelveticaNeue_Medium.otf这样添加
RES /值/ attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomFontTextView">
<attr name="fontStyle" format="enum">
<enum name="poppinsSemiBold" value="1" />
<enum name="helveticaNeueMedium" value="2" />
<enum name="poppinsRegular" value="3" />
</attr>
</declare-styleable>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string" />
</declare-styleable>
</resources>
自定义字体类 TextViewPlus.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import com.offerbuk.R;
import com.offerbuk.app.MyApplication;
public class TextViewPlus extends TextView {
private static final String TAG = "TextViewPlus";
private int fontStyle;
private TypedArray a = null;
public TextViewPlus(Context context) {
super(context);
init(null, 0);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
try {
this.a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, defStyle, 0);
this.fontStyle = a.getInteger(R.styleable.CustomFontTextView_fontStyle, 0);
} finally {
if (this.a != null)
this.a.recycle();
}
if (!isInEditMode()) {
switch (fontStyle) {
case 1:
setTypeface(MyApplication.poppinsSemiBold);
break;
case 2:
setTypeface(MyApplication.helveticaNeueMedium);
break;
case 3:
setTypeface(MyApplication.poppinsRegular);
break;
default:
setTypeface(MyApplication.poppinsRegular);
break;
}
}
}
}
在布局文件
中使用此功能 <TextViewPlus
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:fontStyle="poppinsRegular" />