android7dialog,Android 7.0 Dialog 无法显示的问题

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值