Simple Android toasts not aligning properly

穿精又带淫゛_ 提交于 2019-11-27 23:29:32

问题


I'm simply calling from my Activity:

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?


回答1:


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:

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.

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.



来源:https://stackoverflow.com/questions/21741824/simple-android-toasts-not-aligning-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!