[英]What is the difference between the int used by View.VISIBLE and normal ints?
If I want to toggle the visibility of a TextView, I can use View.VISIBLE or View.INVISIBLE 如果要切换TextView的可见性,可以使用View.VISIBLE或View.INVISIBLE
According to the Android Documentation , VISIBLE is 0 and INVISIBLE is 1. 根据Android文档 ,VISIBLE为0,INVISIBLE为1。
But it doesn't work if I use setvisibility(0) 但是如果我使用setvisibility(0)则不起作用
Why does View.VISIBLE work but not 0? 为什么View.VISIBLE不能工作但不能0?
Checking the source code is always a valid option with Android. 检查源代码始终是Android的有效选项。 One thing that is immediately apparent is that INVISIBLE
is not 1: 显而易见的一件事是, INVISIBLE
不是 1:
/**
* This view is visible. Use with {@link #setVisibility}.
*/
public static final int VISIBLE = 0x00000000;
/**
* This view is invisible, but it still takes up space for layout purposes.
* Use with {@link #setVisibility}.
*/
public static final int INVISIBLE = 0x00000004;
However, VISIBLE
is indeed 0, so using a literal 0 should work. 但是, VISIBLE
确实为0,因此使用文字0应该可行。 All setVisibility()
really does is delegate to setFlags()
with the number you pass it and VISIBILITY_MASK
, which is 0x0C (12). setVisibility()
实际上所做的全部是将传递给它的数字和VISIBILITY_MASK
委托给setFlags()
,它是0x0C(12)。
These int values can change all the time and that's why you need to be careful when using the numeric as opposed to enum-like parameter (I know it not an enum... just saying). 这些int值可以随时更改,这就是为什么在使用数字而不是像枚举之类的参数时要特别小心(我知道这不是枚举……只是说)。
if you really want to know the value behind those parameters use: 如果您真的想知道这些参数背后的值,请使用:
hello.setText(Integer.toString(View.INVISIBLE))
with hello being a TextView. 你好是一个TextView。
in this case, the answer is 4 ( .GONE
is 8) 在这种情况下,答案是4( .GONE
是8)
Best way: 最好的办法:
private void setViewVisiblity(int visiblity){
Button b = findViewById(R.id.btn);
b.setVisibility(visiblity);
}
// for visible:
setViewVisiblity(View.VISIBLE)
// for invisible:
setViewVisiblity(View.INVISIBLE)
// for gone:
setViewVisiblity(View.GONE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.