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
更多相关文章
- Android6.0 Launcher3 修改app字体大小
- Android Studio NDK开发在C代码中将Log输出到logcat上面
- android Edittext内容字体大小动态变化
- Android 同个TextView设置字体大小不一样
- 调整recovery界面菜单字体大小
- Android getDimensionPixelSize, 代码中设置字体大小,读xml配置。
- Android中将Activity转换成View使用.
- Android 单独控件实现不同字体大小、不同颜色
- Android 中将 "base64图片" 解码为".png"图片