如何检查视图在 Android 中是否可见? [英] How can I check if a view is visible or not in Android?

查看:22
本文介绍了如何检查视图在 Android 中是否可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 上将 visibility 设置为不可见:

I set visibility to invisible like this on Android:

myImageView.setVisibility(View.INVISIBLE);

然后让它可见:

myImageView.setVisibility(View.VISIBLE);

现在我不知道 myImageView 是否可见,我该如何检查它:

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?

推荐答案

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

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.INVISIBLE该视图是不可见的,但它通常占用的任何间距仍将被使用.它的隐形"

View.INVISIBLE The view is invisible, but any spacing it would normally take up will still be used. Its "invisible"

View.GONE视野消失了,你看不到它,它不占据点".

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
}

这篇关于如何检查视图在 Android 中是否可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆