Android(安卓)设置字体大小和显示大小后界面混乱
1、显示大小
public static int getDefaultDisplayDensity(int displayId) {
try {
final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
return wm.getInitialDisplayDensity(displayId);
} catch (RemoteException exc) {
return -1;
}
}
public static void setDefaultDisplay(Context context) {
if(Build.VERSION.SDK_INT > 23) {
Configuration origConfig = context.getResources().getConfiguration();
origConfig.densityDpi = getDefaultDisplayDensity(Display.DEFAULT_DISPLAY);//获取手机出厂时默认的densityDpi
context.getResources().updateConfiguration(origConfig, context.getResources().getDisplayMetrics());
}
}
在界面onCreate中调用setDefaultDisplay,强制修改分辨率
2、字体大小
@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;
}
或者在xml布局中将字体修改为dp而不是用sp
更多相关文章
- android p cts camera测试 android.hardware.camera2.cts.Captur
- Android(安卓)Bluetooth 文件接收路径修改方法
- 如何更换Android系统默认字体(Android6.0)
- android host修改
- Android(安卓)实现不同字体颜色的TextView实现代码
- Android修改状态栏颜色
- android -> 修改自动转为大写 的textAllCaps 属性
- Android(安卓)App安装失败错误总结
- Android(安卓)一行代码全局应用内屏蔽系统设置字体和显示大小