本文翻译自:Android : difference between invisible and gone?
View可见性状态的invisible
和gone
什么区别?
参考:https://stackoom.com/question/mUOt/Android-隐形和消失之间的区别
INVISIBLE: 无形:
This view is invisible, but it still takes up space for layout purposes. 此视图不可见,但它仍占用布局空间。
GONE: GONE:
This view is invisible, and it doesn't take any space for layout purposes. 此视图不可见,并且不需要任何空间用于布局。
For ListView or GridView there is an another difference, when visibility initially set to 对于ListView或GridView,当可见性初始设置为时,还有另一个区别
INVISIBLE: 无形:
Adapter's getView() function called 调用适配器的getView()函数
GONE: GONE:
Adapter's getView() function didn't call, thus preventing views to load, when it is unnecessary 适配器的getView()函数没有调用,因此在不需要时阻止视图加载
From Documentation you can say that 从文档中你可以这么说
View.GONE This view is invisible, and it doesn't take any space for layout purposes. View.GONE此视图不可见,并且不需要任何空间用于布局。
View.INVISIBLE This view is invisible, but it still takes up space for layout purposes. View.INVISIBLE此视图不可见,但它仍占用布局空间。
Lets clear the idea with some pictures. 让我们用一些图片清除这个想法。
Assume that you have three buttons, like below 假设您有三个按钮,如下所示
Now if you set visibility of Button Two as invisible ( View.INVISIBLE
), then output will be 现在,如果将Button Two的可见性设置为不可见( View.INVISIBLE
),则输出将为
And when you set visibility of Button Two as gone ( View.GONE
) then output will be 当你将Button Two的可见性设置为已消失( View.GONE
)时,输出将是
Hope this will clear your doubts. 希望这会清除你的疑虑。
INVISIBLE: 无形:
The view has to be drawn and it takes time. 必须绘制视图,这需要时间。
GONE: GONE:
The view doesn't have to be drawn. 不必绘制视图。
I'd like to add to the right and successful answers, that if you initialize a view with visibility as View.GONE
, the view could have been not initialized and you will get some random errors. 我想添加正确和成功的答案,如果您将可见性初始化为View.GONE
,则视图可能未初始化,您将收到一些随机错误。
For example if you initialize a layout as View.GONE
and then you try to start an animation, from my experience I've got my animation working randomly times. 例如,如果您将布局初始化为View.GONE
,然后尝试启动动画,根据我的经验,我的动画随机工作。 Sometimes yes, sometimes no. 有时是,有时没有。
So before handling (resizing, move, whatever) a view, you have to init it as View.VISIBLE
or View.INVISIBLE
to render it (draw it) in the screen, and then handle it. 因此,在处理(调整大小,移动等)视图之前,必须将其初始化为View.VISIBLE
或View.INVISIBLE
以在屏幕中呈现它(绘制它),然后处理它。