我是Android开发的新手,所以我正在开发一个简单的应用程序,该应用程序可以通过按一些按钮来隐藏textview,因此在Java代码中,按钮的OnClick事件方法中,我将textview设置为不可见,我使用了:
textView.setVisibility(1);
因为1是android reference中描述的“不可见”的值,但是它不起作用,所以在我使用之后
textView.setVisibility(View.INVISIBLE);
并且有效,那么何时使用“ 1”值?以及为什么View.INVISIBLE = 4而不是android引用所说的1?
在android参考中,我可以看到属性android:visibility的值Invisible定义为1
最佳答案
这是一个好问题,我检查了Android源代码(frameworks / base / core / java / android / view / View.java)
case com.android.internal.R.styleable.View_visibility:
final int visibility = a.getInt(attr, 0);
if (visibility != 0) {
viewFlagValues |= VISIBILITY_FLAGS[visibility]; //here is the key to your question
viewFlagMasks |= VISIBILITY_MASK;
}
break;
以下是VISIBILITY_FLAGS的内容:
private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
数组元素的值实际上是Android Reference中显示的值
/**
* This view is visible.
* Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
* android:visibility}.
*/
public static final int VISIBLE = 0x00000000;
/**
* This view is invisible, but it still takes up space for layout purposes.
* Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
* android:visibility}.
*/
public static final int INVISIBLE = 0x00000004;
/**
* This view is invisible, and it doesn't take any space for layout
* purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
* android:visibility}.
*/
public static final int GONE = 0x00000008;
因此,即使您在清单文件中使用android:invisible,Android框架最终也会调用值为4的setVisibility(...)。