Android 通过单击视图外部使视图消失 [英] Android make view disappear by clicking outside of it
问题描述
我有一些在按下按钮时可见的视图.如果我在这些视图之外单击,我希望它们消失.
I have some views that I make visible upon a button press. I want them to disappear if I click outside of those views.
这将如何在 Android 上完成?
How would this be done on Android?
此外,我意识到后退按钮"也可以帮助 Android 用户解决这个问题——我可能会用它作为关闭视图的辅助方式——但有些平板电脑甚至没有使用物理"后退按钮已经非常不重视了.
Also, I realize that the "back button" can also assist Android users with this - I might use that as a secondary way to close the views - but some of the tablets aren't even using a 'physical' back button anymore, it has been very de-emphasized.
推荐答案
一个简单/愚蠢的方法:
An easy/stupid way:
创建一个虚拟的空视图(假设没有源的 ImageView),让它填充父视图
Create a dummy empty view (let's say ImageView with no source), make it fill parent
如果它被点击,那么做你想做的事.
If it is clicked, then do what you want to do.
您需要将 XML 文件中的根标记设为 RelativeLayout.它将包含两个元素:您的虚拟视图(将其位置设置为 align the Parent Top
).另一个是包含视图和按钮的原始视图(此视图可能是 LinearLayout 或您制作的任何内容.不要忘记将其位置设置为 align the Parent Top
)
You need to have the root tag in your XML file to be a RelativeLayout. It will contain two element: your dummy view (set its position to align the Parent Top
). The other one is your original view containing the views and the button (this view might be a LinearLayout or whatever you make it. don't forget to set its position to align the Parent Top
)
希望对你有帮助,祝你好运!
Hope this will help you, Good Luck !
这篇关于Android 通过单击视图外部使视图消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!