如何检查 Android 中的视图是否可见?
我在 Android 上将 visibility
设置为不可见:
myImageView.setVisibility(View.INVISIBLE);
然后使其可见:
myImageView.setVisibility(View.VISIBLE);
现在我不知道 myImageView
是否可见,我该如何检查它这:
if (myImageView IS VISIBLE) {
Do something
} else {
Do something else
}
我该怎么做?我必须在括号内写什么?
I set visibility
to invisible like this on Android:
myImageView.setVisibility(View.INVISIBLE);
And then to make it visible:
myImageView.setVisibility(View.VISIBLE);
Now I don't know if myImageView
is visible or not, how can I check it like this:
if (myImageView IS VISIBLE) {
Do something
} else {
Do something else
}
How can I do that? What do I have to write within the brackets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然 View.getVisibility() 确实获得了可见性,但它不是简单的 true/false。视图的可见性可以设置为以下三项之一。
查看.VISIBLE
景色可见。
查看.INVISIBLE
该视图是不可见的,但它通常占用的任何间距仍将被使用。它的“隐形”
View.GONE
景色消失了,你看不到它,它也不占据“位置”。
因此,要回答您的问题,您需要寻找:
Although View.getVisibility() does get the visibility, its not a simple true/false. A view can have its visibility set to one of three things.
View.VISIBLE
The view is visible.
View.INVISIBLE
The view is invisible, but any spacing it would normally take up will still be used. Its "invisible"
View.GONE
The view is gone, you can't see it and it doesn't take up the "spot".
So to answer your question, you're looking for:
或者你可以简单地使用
Or you could simply use
如果图像是布局的一部分,它可能是“View.VISIBLE”,但这并不意味着它在可见屏幕的范围内。如果这就是你所追求的;这会起作用:
If the image is part of the layout it might be "View.VISIBLE" but that doesn't mean it's within the confines of the visible screen. If that's what you're after; this will work:
您可以使用相应的方法 getVisibility()。以“get”和“set”为前缀的方法名称是 Java 表示属性的约定。有些语言具有实际的属性语言结构,但 Java 不是其中之一。因此,当您看到标有“setX”的内容时,您可以 99% 确定有相应的“getX”来告诉您该值。
You'd use the corresponding method getVisibility(). Method names prefixed with 'get' and 'set' are Java's convention for representing properties. Some language have actual language constructs for properties but Java isn't one of them. So when you see something labeled 'setX', you can be 99% certain there's a corresponding 'getX' that will tell you the value.