自定义UI控件
系统是如何定义UI控件的?
1. 首先我们在布局文件中定义了一个布局
我们自定义的布局(其中声明了命名空间为xmlns后面的内容)
<View xmlns:android="/apk/res/android"android:id="@+id/view"android:visibility="visible"android:layout_width="match_parent"android:layout_height="wrap_content" />
View的属性其实是在attrs.xml中声明的
<?xml version="1.0" encoding="utf-8"?><resources>......<declare-styleable name="View"><attr name="id" format="reference" /><attr name="visibility"><!-- Visible on screen; the default value. --><enum name="visible" value="0" /><!-- Not displayed, but taken into account during layout (space is left for it). --><enum name="invisible" value="1" /><!-- Completely hidden, as if the view had not been added. --><enum name="gone" value="2" /></attr></declare-styleable>......</resources>
2. 接着,让我们看看View类是如何获取我们在布局中定义的属性的值
在View.java的构造方法中是这样的
public View(Context context, AttributeSet attrs, int defStyle) {this(context);TypedArray a = context.obtainStyledAttributes(attrs,com.android.internal.R.styleable.View, defStyle, 0);final int N = a.getIndexCount