快速使用反射更換Android全局字體
問題背景
之前在編寫鯉魚日語時,因為使用了外部的字體,這導致了一個問題就是我的ListView顯示的是圓滑的字體,但是其他dialog、Spinner就全都是Android默認字體,這樣不統一就會很難看。根據拿來主義,我找到了一個利用反射更換全局字體的方法,而且不需要使用外部庫,夠我用了。
代碼已經上傳GitHub(字體自己下載吧,太大了)
新建繼承Application類的SetAppTypeface
因為是全局更換字體,所以需要使用Application來完成全局的作用:
android.app.Application
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.
說明比較易懂,就是他可以控制一個app全局的狀態,還提示可以在
AndroidManifest.xml's <application> tag這個標簽中進行設置。
以下是SetAppTypeface.java全部代碼
package pri.weiqiang.frontinstead;
import java.lang.reflect.Field;
import android.app.Application;
import android.graphics.Typeface;
/**
- @author 54wall
- @date 創建時間:2016-7-28 下午2:20:59
@version 1.0
*/
public class SetAppTypeface extends Application{
public static Typeface typeFace;
@Override
public void onCreate() {
super.onCreate();
setTypeface();
}
public void setTypeface(){
//華文彩云,加載外部字體assets/front/huawen_caiyun.ttf
typeFace = Typeface.createFromAsset(getAssets(), "fonts/huawen_caiyun.ttf");
try
{
//與values/styles.xml中的<item name="android:typeface">sans</item>對應
// Field field = Typeface.class.getDeclaredField("SERIF");
// field.setAccessible(true);
// field.set(null, typeFace);
// Field field_1 = Typeface.class.getDeclaredField("DEFAULT");
// field_1.setAccessible(true);
// field_1.set(null, typeFace);
//與monospace對應
// Field field_2 = Typeface.class.getDeclaredField("MONOSPACE");
// field_2.setAccessible(true);
// field_2.set(null, typeFace);
//與values/styles.xml中的<item name="android:typeface">sans</item>對應
Field field_3 = Typeface.class.getDeclaredField("SANS_SERIF");
field_3.setAccessible(true);
field_3.set(null, typeFace);
}
catch (NoSuchFieldException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}</code></pre>
Field這個類就是反射了,他可以獲取相應字段,然后就可以通過反射設置不能設置的屬性了(具體其實我也不是很懂)。
為什么是SERIF-MONOSPACE-SANS_SERIF這幾個字符串
直接進入Typeface這個類,你會發現:
static {
init();
// Set up defaults and typefaces exposed in public API
DEFAULT = create((String) null, 0);
DEFAULT_BOLD = create((String) null, Typeface.BOLD);
SANS_SERIF = create("sans-serif", 0);
SERIF = create("serif", 0);
MONOSPACE = create("monospace", 0);
sDefaults = new Typeface[] {
DEFAULT,
DEFAULT_BOLD,
create((String) null, Typeface.ITALIC),
create((String) null, Typeface.BOLD_ITALIC),
};
}</code></pre>
create就是在加載字體了。而SetAppTypeface這個類繼承自Application,Android App啟動后會首先加載這個類,實現反射替換App中的全部使用SANS_SERIF的字體,這樣配合values/styles.xml進行全局默認字體樣式選擇就可以進行全局字體的更換了。
修改values/styles.xml
在styles.xml文件中找到<style name="AppTheme" parent="AppBaseTheme">并修改成下邊的
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:typeface">sans</item>
</style>
這里android:typeface可以設置的僅僅有normal、sans、serif、monospace可以設置,因為我在SetAppTypeface類中設置的是Typeface.class.getDeclaredField("SANS_SERIF");
所以我這里便設置成sans,如果getDeclaredField()設置的是其他的類型,則要選擇同類型的其他諸如serif、monospace等等
修改AndroidManifest.xml
進入AndroidManifest.xml找到application這個tag,直接在其內部增加android:name=".SetAppTypeface",完成后如下:
<application
android:allowBackup="true"
**android:name=".SetAppTypeface"**
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
上邊三步完成后就可以啟動App看看效果了:
我的MainActivity如下:
package pri.weiqiang.frontinstead;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
/**
* @author 54wall
* @date 創建時間:2016-7-28 下午2:20:59
* @version 1.0
*/
public class MainActivity extends Activity {
public TextView textView01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView01=(TextView)super.findViewById(R.id.TextView01);
textView01.setTypeface(null, Typeface.NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
簡單說一下,顯示內容其實在布局文件activity_main中,這里設置其中一個TextView字體風格為Typeface.NORMAL,這樣的話,因為他不在默認使用styles.xml中的sans字體,所以全局對他來說便沒有作用了。
效果如下:

快速使用反射更換Android全局字體.jpg
來自:http://www.jianshu.com/p/89f62f5254b7