app 在 Android 7.0 上登录的时候, Dialog 不显示了,但是半透明背景显示
经过搜索和对比,发现出现该问题是因为重写了 getResources() 方法造成的 。
1 /**
2 * 设置 app 不随着系统字体的调整而变化3 */
4 @Override5 publicResources getResources() {6 Resources resources = super.getResources();7 Configuration configuration =resources.getConfiguration();8 configuration.setToDefaults();9 resources.updateConfiguration(configuration, resources.getDisplayMetrics());10 returnresources;11 }
configuration.setToDefault() 方法
1 /**
2 * Set this object to the system defaults.3 */
4 public voidsetToDefaults() {5 fontScale = 1;6 mcc = mnc = 0;7 locale = null;8 userSetLocale = false;9 touchscreen =TOUCHSCREEN_UNDEFINED;10 keyboard =KEYBOARD_UNDEFINED;11 keyboardHidden =KEYBOARDHIDDEN_UNDEFINED;12 hardKeyboardHidden =HARDKEYBOARDHIDDEN_UNDEFINED;13 navigation =NAVIGATION_UNDEFINED;14 navigationHidden =NAVIGATIONHIDDEN_UNDEFINED;15 orientation =ORIENTATION_UNDEFINED;16 screenLayout =SCREENLAYOUT_UNDEFINED;17 uiMode =UI_MODE_TYPE_UNDEFINED;18 screenWidthDp = compatScreenWidthDp =SCREEN_WIDTH_DP_UNDEFINED;19 screenHeightDp = compatScreenHeightDp =SCREEN_HEIGHT_DP_UNDEFINED;20 smallestScreenWidthDp = compatSmallestScreenWidthDp =SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;21 densityDpi =DENSITY_DPI_UNDEFINED;22 seq = 0;23 }
在 Android 7.0 上的 PhoneWindow 代码有了变化, DecorView 从 PhoneWindow 中独立出来一个类,同时在初始化的时候,调用了 updateAvailableWidth() 方法
1 private voidupdateAvailableWidth() {
2 Resources res =getResources();
3 mAvailableWidth =TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4 res.getConfiguration().screenWidthDp, res.getDisplayMetrics());
5 }
其中会调用 getResources() 方法,但是该方法设置为 setToDefault(),那么获取到的 screenWidthDp 为默认值 SCREEN_WIDTH_DP_UNDEFINED
1 /**
2 * Default value for {@link#screenWidthDp} indicating that no width3 * has been specified.4 */
5 public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
代码中默认值为 0 ,那么 mAvaliableWidth 值为 0 ,所以造成 Dialog 无法显示的问题。
解决办法:
只设置 getResource() 方法中的 fontScale 为 1 ,其他的值不修改。
1 /**
2 * 设置 app 不随着系统字体的调整而变化3 */
4 @Override5 publicResources getResources() {6 Resources resources = super.getResources();7 Configuration configuration =resources.getConfiguration();8 configuration.fontScale = 1;9 resources.updateConfiguration(configuration, resources.getDisplayMetrics());10 returnresources;11 }
参考:http://www.jianshu.com/p/9be83be8d1ef
原文:http://www.cnblogs.com/liyiran/p/6491001.html