Last active
December 21, 2015 08:19
-
-
Save yaoxinghuo/6277717 to your computer and use it in GitHub Desktop.
Android 整个应用换字体,字体ttf 文件放到 assets 下,调用可以放到 Application 的 onCreate 里面
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setDefaultFont() { | |
try { | |
final Typeface bold = Typeface.createFromAsset(getAssets(), | |
"stheitilight.ttf"); | |
final Typeface italic = Typeface.createFromAsset(getAssets(), | |
"stheitilight.ttf"); | |
final Typeface boldItalic = Typeface.createFromAsset(getAssets(), | |
"stheitilight.ttf"); | |
final Typeface regular = Typeface.createFromAsset(getAssets(), | |
"stheitilight.ttf"); | |
Field DEFAULT = Typeface.class.getDeclaredField("DEFAULT"); | |
DEFAULT.setAccessible(true); | |
DEFAULT.set(null, regular); | |
Field DEFAULT_BOLD = Typeface.class | |
.getDeclaredField("DEFAULT_BOLD"); | |
DEFAULT_BOLD.setAccessible(true); | |
DEFAULT_BOLD.set(null, bold); | |
Field sDefaults = Typeface.class.getDeclaredField("sDefaults"); | |
sDefaults.setAccessible(true); | |
sDefaults.set(null, new Typeface[] { regular, bold, italic, | |
boldItalic }); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
修改 ActionBar 的字体: | |
try { | |
Integer titleId = (Integer) Class.forName("com.android.internal.R$id") | |
.getField("action_bar_title").get(null); | |
TextView title = (TextView) getWindow().findViewById(titleId); | |
// check for null and manipulate the title as see fit | |
} catch (Exception e) { | |
Log.e(TAG, "Failed to obtain action bar title reference"); | |
} | |
P.S. Based on @pjv comment there's a better way to find action bar title id | |
final int titleId = | |
Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { | |
View view = super.onCreateView(parent, name, context, attrs); | |
return setCustomTypeFaceIfNeeded(name, attrs, view); | |
} | |
protected View setCustomTypeFaceIfNeeded(String name, AttributeSet attrs, View view) { | |
View result = null; | |
if ("TextView".equals(name)) { | |
result = new TextView(this, attrs); | |
((TextView) result).setTypeface(helloTypeface); | |
} | |
if ("EditText".equals(name)) { | |
result = new EditText(this, attrs); | |
((EditText) result).setTypeface(helloTypeface); | |
} | |
if ("Button".equals(name)) { | |
result = new Button(this, attrs); | |
((Button) result).setTypeface(helloTypeface); | |
} | |
if (result == null) { | |
return view; | |
} else { | |
if (ENABLE_FONT_LOGGING) { | |
Log.v(FONT_LOG_CAT_TAG, "A type face was set on " + result.getId()); | |
} | |
return result; | |
} | |
} |
修改 ActionBar 的字体:
try {
Integer titleId = (Integer) Class.forName("com.android.internal.R$id")
.getField("action_bar_title").get(null);
TextView title = (TextView) getWindow().findViewById(titleId);
// check for null and manipulate the title as see fit
} catch (Exception e) {
Log.e(TAG, "Failed to obtain action bar title reference");
}
P.S. Based on @pjv comment there's a better way to find action bar title id
final int titleId =
Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个要奏效一点
@OverRide
public View onCreateView(String name, Context context, AttributeSet attrs) {
View view = super.onCreateView(name, context, attrs);
return setCustomTypeFaceIfNeeded(name, attrs, view);
}