如何检查 Android 中的视图是否可见?

发布于 2024-09-24 13:07:27 字数 391 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反话 2024-10-01 13:07:27

虽然 View.getVisibility() 确实获得了可见性,但它不是简单的 true/false。视图的可见性可以设置为以下三项之一。

查看.VISIBLE
景色可见。

查看.INVISIBLE
该视图是不可见的,但它通常占用的任何间距仍将被使用。它的“隐形”

View.GONE
景色消失了,你看不到它,它也不占据“位置”。

因此,要回答您的问题,您需要寻找:

if (myImageView.getVisibility() == View.VISIBLE) {
    // Its visible
} else {
    // Either gone or invisible
}

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:

if (myImageView.getVisibility() == View.VISIBLE) {
    // Its visible
} else {
    // Either gone or invisible
}
过气美图社 2024-10-01 13:07:27

或者你可以简单地使用

View.isShown()

Or you could simply use

View.isShown()
时光是把杀猪刀 2024-10-01 13:07:27

如果图像是布局的一部分,它可能是“View.VISIBLE”,但这并不意味着它在可见屏幕的范围内。如果这就是你所追求的;这会起作用:

Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
    // imageView is within the visible window
} else {
    // imageView is not within the visible window
}

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:

Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
    // imageView is within the visible window
} else {
    // imageView is not within the visible window
}
離殇 2024-10-01 13:07:27

您可以使用相应的方法 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文