Android Layout Xml的命名空间

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中这个数组是怎么玩的,还不清楚,不过对于命名空间和自定义属性应该是很清晰了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值