Android 如何隐藏图形布局中的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10403020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:35:11  来源:igfitidea点击:

How to hide elements in graphical layout?

androideclipseandroid-layout

提问by eric.itzhak

I have items in my View which are displayd on click and does not show up on that start of the activity. Now everything works fine but when i use the graphical layout tool i see those items and it makes working with it harder.

我的视图中有项目在单击时显示,但在活动开始时不显示。现在一切正常,但是当我使用图形布局工具时,我看到了这些项目,这使得使用它变得更加困难。

Meaning, i have a ListView with items, i see those items on top of everything.

意思是,我有一个包含项目的 ListView,我看到这些项目位于一切之上。

How can i hide those elements? setting the visibility programaticlly dosen't help. Should i do something like setting alpha to 0 and then add it instead of making items invisible? Seems like a dumb idea, there must be some option i'm missing.

我怎样才能隐藏这些元素?以编程方式设置可见性无济于事。我应该做一些像将 alpha 设置为 0 然后添加它而不是使项目不可见的事情吗?似乎是一个愚蠢的想法,我一定缺少某些选项。

Help anyone?

帮助任何人?

Thanks, Eric

谢谢,埃里克

回答by MH.

Why not set the visibility to 'gone': android:visibility="gone"? That will prevent layout items from showing up andtaking up space in the layout.

为什么不将可见性设置为“消失”:android:visibility="gone"?这将防止布局项出现占用布局中的空间。

Alternatively, if some elements in your view hierarchy may never be shown to the user at all, you could use ViewStubs to 'lazily' load them at runtime. A ViewStubis simply a placeholder for some other view (hierarchy) and is empty and zero-sized by itself. It's a good way of separating your view hierarchy into default and optional elements, and can potentialy reduce the complexity of the view hierarchy at runtime significantly.

或者,如果您的视图层次结构中的某些元素可能永远不会向用户显示,您可以使用ViewStubs 在运行时“延迟”加载它们。AViewStub只是其他视图(层次结构)的占位符,它本身是空的且大小为零。这是将视图层次结构分为默认元素和可选元素的好方法,并且可以在运行时显着降低视图层次结构的复杂性。

回答by ashack

If you want to hide the element programmatically / conditionally instead of in the layout XML as suggested above and setting the visibility wasn't working, perhaps your syntax was wrong? This is how you programmatically hide an element:

如果您想以编程方式/有条件地隐藏元素而不是按照上面的建议在布局 XML 中隐藏元素并且设置可见性不起作用,那么您的语法可能是错误的?这是您以编程方式隐藏元素的方式:

element.setVisibility(View.GONE);