1.xmlns:android="http://schemas.android.com/apk/res/android"
这个是android原生的默认xml的命名空间,名字是android,这个命名空间对应的framework文件是frameworks/base/core/res/res/values/attrs.xml
这个attrs中定义了android各个原生控件中的所有属性,比如TextView的textSize属性,使用的时候能就使用命名空间+:+属性值,然后给赋值来使用
如上面这个TextViw的textSize使用在<Textview 下 android:textSize="20sp" (命名空间:属性 =“属性赋值”)
2.xmlns:custom="http://schemas.android.com/apk/res/项目包名"
2.1这个是自定义的属性的命名空间,名字是custom,这个名字可以随便取值,用自定义属性的时候要以这个名字为前缀
2.1这个命名空间对应的属性文件是res/values/attrs.xm 在该文件中定义自定义属性
<declare-styleable name="NavSwitch">(这个名字可以任意,只有在自定控件的java文件中引入即可)
<attr name="backgroundonimage" format="reference" />
<attr name="backgroundoffimage" format="reference" />
</declare-styleable>
2.2如果我们想在自定义的某一个控件中使用这自定义的属性,在java文件的构造方法中引入即可
public UICheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavSwitch);(引入)
bg_on = (Drawable)a.getDrawable(R.styleable.NavSwitch_backgroundonimage);
bg_off = (Drawable) a.getDrawable(R.styleable.NavSwitch_backgroundoffimage);
a.recycle();
}
2.3.然后我们就可以用这个自定义的属性在xml文件中使用了,要以custom这个自定义空间开头
2.4我们在2.3中的配置,在2.2中的自定义控件中把值给解析出来了,然后要怎么做看需求了
2.5其实在2.1中自定义的属性是在R.java中添加了一个int类型的数组
public static final class styleable {
/** Attributes that can be used with a NavSwitch.
<p>Includes the following attributes:</p>
<table>
<colgroup align="left" />
<colgroup align="left" />
<tr><th>Attribute</th><th>Description</th></tr>
<tr><td><code>{@link #NavSwitch_backgroundoffimage cn.nish.naw.angel:backgroundoffimage}</code></td><td></td></tr>
<tr><td><code>{@link #NavSwitch_backgroundonimage cn.nish.naw.angel:backgroundonimage}</code></td><td></td></tr>
</table>
@see #NavSwitch_backgroundoffimage
@see #NavSwitch_backgroundonimage
*/
public static final int[] NavSwitch = {
0x7f01001a, 0x7f01001b
};
至于个R.java中这个数组是怎么玩的,还不清楚,不过对于命名空间和自定义属性应该是很清晰了
这个是android原生的默认xml的命名空间,名字是android,这个命名空间对应的framework文件是frameworks/base/core/res/res/values/attrs.xml
这个attrs中定义了android各个原生控件中的所有属性,比如TextView的textSize属性,使用的时候能就使用命名空间+:+属性值,然后给赋值来使用
如上面这个TextViw的textSize使用在<Textview 下 android:textSize="20sp" (命名空间:属性 =“属性赋值”)
2.xmlns:custom="http://schemas.android.com/apk/res/项目包名"
2.1这个是自定义的属性的命名空间,名字是custom,这个名字可以随便取值,用自定义属性的时候要以这个名字为前缀
2.1这个命名空间对应的属性文件是res/values/attrs.xm 在该文件中定义自定义属性
<declare-styleable name="NavSwitch">(这个名字可以任意,只有在自定控件的java文件中引入即可)
<attr name="backgroundonimage" format="reference" />
<attr name="backgroundoffimage" format="reference" />
</declare-styleable>
2.2如果我们想在自定义的某一个控件中使用这自定义的属性,在java文件的构造方法中引入即可
public UICheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavSwitch);(引入)
bg_on = (Drawable)a.getDrawable(R.styleable.NavSwitch_backgroundonimage);
bg_off = (Drawable) a.getDrawable(R.styleable.NavSwitch_backgroundoffimage);
a.recycle();
}
2.3.然后我们就可以用这个自定义的属性在xml文件中使用了,要以custom这个自定义空间开头
2.4我们在2.3中的配置,在2.2中的自定义控件中把值给解析出来了,然后要怎么做看需求了
2.5其实在2.1中自定义的属性是在R.java中添加了一个int类型的数组
public static final class styleable {
/** Attributes that can be used with a NavSwitch.
<p>Includes the following attributes:</p>
<table>
<colgroup align="left" />
<colgroup align="left" />
<tr><th>Attribute</th><th>Description</th></tr>
<tr><td><code>{@link #NavSwitch_backgroundoffimage cn.nish.naw.angel:backgroundoffimage}</code></td><td></td></tr>
<tr><td><code>{@link #NavSwitch_backgroundonimage cn.nish.naw.angel:backgroundonimage}</code></td><td></td></tr>
</table>
@see #NavSwitch_backgroundoffimage
@see #NavSwitch_backgroundonimage
*/
public static final int[] NavSwitch = {
0x7f01001a, 0x7f01001b
};
至于个R.java中这个数组是怎么玩的,还不清楚,不过对于命名空间和自定义属性应该是很清晰了