android toast居中显示_Toast文字不居中

当你在API19以上定义沉浸式风格的UI时,可能会定义整个App的Theme为:

true

true

true

这样的话Toast的文字便不会居中显示,而在背景框中靠上的位置。

随手搜一下,会得到如下答案:

将fitsSystemWindows设置为false。

(WTF,我要是能设置成false就不在AppTheme中指定为true了。)

将Toast.makeText(context, msg, length)中第一个参数的context使用Application的Context而不是使用Activity的context。

(结果是居中了,可是Toast的样式却也变了。)

解决方案

问题的原因在于将fitsSystemWindows用错了地方。fitsSystemWindows应该定义在View的Layout文件中,而不是定义在Theme里。

The android:fitsSystemWindows attribute is intended for usage on views in layout xml, not in themes.

What you're seeing is the effect of the way the styled attribute system works in Android. If no attribute is specified on the view element or in the explicit style given to the view, the framework checks to see if that attribute has been specified on the theme itself. If it is found there, that value is used. Since the views used by toasts use your activity's theme, the default value of false is overridden and you see this behavior.

You're not just changing the fitsSystemWindows default for your top-level views by specifying it in the theme, you're overriding it for all views with that theme, which isn't what you want. You should only specify fitsSystemWindows on views within your layouts or in styles that you explicitly apply to views within your layouts, not on themes.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值