字體大小
在BaseActivity和BaseApplication裏重寫方法或者在基類裏重寫方法:
/設置字體爲默認大小,不隨系統字體大小改而改變
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.fontScale != 1)//非默認值
getResources();
super.onConfigurationChanged(newConfig);
}
@Override
public Resources getResources() {
Resources res = super.getResources();
if (res.getConfiguration().fontScale != 1) {//非默認值
Configuration newConfig = new Configuration();
newConfig.setToDefaults();//設置默認
res.updateConfiguration(newConfig, res.getDisplayMetrics());
}
return res;
}
字體樣式
使用批量設置字體框架 Calligraphy
在build.gradle文件中添加:
implementation 'uk.co.chrisjenx:calligraphy:2.3.0'//設置字體樣式
1.首先要在自己定義的Appliction類中的OnCreate方法中添加如下語句
//設置字體樣式
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
// .setDefaultFontPath("fonts/FZSong.ttf")
.setDefaultFontPath("fonts/Roboto-Regular.ttf")
.setFontAttrId(R.attr.fontPath)
.addCustomViewWithSetTypeface(CustomViewWithTypefaceSupport.class)
.addCustomStyle(TextField.class, R.attr.textFieldStyle)
.build()
);
2.定義一個BaseActivity類,所有的Activity都繼承該類,然後添加如下方法(複寫的)
//設置字體樣式
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
3.把字體包是放在assets目錄下的fonts文件夾下
4.在工程的build.gradle添加:
version = getProperty('VERSION_NAME')
ext {
isReleaseVersion = has("release")
versionCodeInt = getProperty('VERSION_CODE').toInteger()
supportLibraryVersion = '27.1.1'
buildToolsVersion = '27.0.3'
compileSdkVersion = 27
minSdkVersion = 14
targetSdkVersion = 27
}
*考慮字體版權關係(方正和微軟雅黑萬萬不能用會被追究起訴),字體使用時選擇了思源黑體和思源宋體,思源字體確實好看可惜都會有行間距過大問題,下載的很多稱修正版的都是假的,無奈只能捨棄,後來找到了一個破產公司fandol的字體也還不錯,雖然最後領導沒有采用(最後用的是“Roboto-Regular.ttf”)
最後附上一些字體,請見個人下載資源頁面