??運(yùn)營(yíng)反饋,老年用戶的手機(jī)多設(shè)置為大字體,在使用我們app過(guò)程中,由于字體被放大,導(dǎo)致布局錯(cuò)亂,部分功能按鍵遮擋,無(wú)法正常使用。
??收到問(wèn)題,著手解決,除了對(duì)界面布局進(jìn)行改寫,改為約束布局,對(duì)app字體大小也進(jìn)行統(tǒng)一管理,然而這不是主要的,最主要的是避免系統(tǒng)更改app字體的大小。
??Android提供了相關(guān)的方法用來(lái)實(shí)現(xiàn)
public class DisplayUtil {
/**
* 保持字體大小不隨系統(tǒng)設(shè)置變化(用在界面加載之前)
* 要重寫Activity的attachBaseContext()
*/
public static Context attachBaseContext(Context context, float fontScale) {
Configuration config = context.getResources().getConfiguration();
//正確寫法
config.fontScale = fontScale;
return context.createConfigurationContext(config);
}
/**
* 保持字體大小不隨系統(tǒng)設(shè)置變化(用在界面加載之前)
* 要重寫Activity的getResources()
*/
public static Resources getResources(Context context, Resources resources, float fontScale) {
Configuration config = resources.getConfiguration();
if(config.fontScale != fontScale) {
config.fontScale = fontScale;
return context.createConfigurationContext(config).getResources();
} else {
return resources;
}
}
/**
* 保存字體大小,后通知界面重建,它會(huì)觸發(fā)attachBaseContext,來(lái)改變字號(hào)
*/
public static void recreate(Activity activity) {
activity.recreate();
}
}
??在BaseActivity中復(fù)寫相關(guān)的方法:
static float fontScale = 1f;
@Override
public Resources getResources() {
Resources resources = super.getResources();
return DisplayUtil.getResources(this,resources,fontScale);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(DisplayUtil.attachBaseContext(newBase,fontScale));
}
public void setFontScale(float fontScale) {
this.fontScale = fontScale;
DisplayUtil.recreate(this);
}
經(jīng)測(cè)試,在設(shè)置中把字體調(diào)整為最大,打開(kāi)app,app中字體均保持不變,測(cè)試華為,vivo,小米機(jī)型均生效,于是交付測(cè)試。