简单的Andr​​oid敬酒不对准正确 [英] Simple Android toasts not aligning properly

查看:132
本文介绍了简单的Andr​​oid敬酒不对准正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从我的活动调用

Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show()

但结果是在烤面包容器的顶部对齐的文本,而不是居中内,因为它应该:

But the result is a text aligned on the top of the toast container, not centered inside as it should:

这是什么可能是错的任何想法?

Any ideas on what could be wrong?

推荐答案

我设法解决它。问题就出在应用属性安卓fitsSystemWindows 来活动的主题。我发现这个答案这可以解释为什么不应该做的:

I managed to fix it. The problem lies in applying the attribute android:fitsSystemWindows to the theme of an activity. I found this answer that explains why that should not be done:

机器人:fitsSystemWindows 属性适用于使用的意见在布局XML,而不是在主题

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

你看到的是一路的风格属性的效果   系统工作在Android系统。如果没有属性上的视图中指定   元件或在给定视图显式风格,框架   检查是否该属性已经指定的主题   本身。如果发现有,则使用该值。由于使用的意见   由祝酒词使用活动的主题是假的默认值是   覆盖时看到此行为。

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.

你不只是改变了fitsSystemWindows默认为您   在主题指定它顶级的观点,你重写它   为的与主题,这是不是你想要的一切的意见。你应该   只有在指定的范围内布局或意见fitsSystemWindows   样式,您明确地应用到你的观点的布局中,而不是   主题。

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.

刚刚申请的属性,最上面的的ViewGroup 的活动(或者风格吧),而不是它的主题和吐司将格式正确无误。

Just apply the attribute to the topmost ViewGroup of the activity (or style it) instead of its theme and the toast will be formatted correctly.

这篇关于简单的Andr​​oid敬酒不对准正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆