这是一个很好的问题,我检查了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;
所以即使你在 manifest 文件中使用 android:invisible,Android 框架最终还是会调用值为 4 的 setVisibility(...)。