android 中View对象的创建可以在代码中创建,也可以在布局文件中声明,在布局文件中声明时,可以对在布局文件中添加属性,如:
Xml代码
  1. android:layout_width="fill_parent"

属性包括两个部分:android和layout_width,android是命名空间,layout_width是属性名,我们可以在View上添加任意不带前缀的属性如:
Xml代码
  1. <Viewandroid:layout_width="44dip"android:layout_height="0dip"anyproperty="value"/>

上面的代码不会报任何的错误,但是这样做并没有任何的实际意义。
View中要想自己生命的属性有意义,则需要为属性加一个命名空间前缀,在布局文件中直接给View加前缀是不允许的,如:<Button myxmlns:anyproperty="value" /> 这样的代码在IDE中会直接报错,并提示无该命名空间,要想使得该命名空间有效,则需要在使用该命名空间之前生命该命名空间,方式如下:

Xml代码
  1. <Viewxmlns:myxmlns="ssss"/>

有了如上的生命,我们就可以有如下的代码而使得IDE不会报告任何错误:
Xml代码
  1. <Viewxmlns:myxmlns="sss"myxmlns:anyproperty="value"/>

在实际应用中大多数的自定义命名空间都声明在第一个元素中,如:
Xml代码
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:myxmlns="ssss"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. ss=""/>
  12. <Viewmyxmlns:sss="sss"/>
  13. </LinearLayout>

但这些声明不会使得IDE报错,但其实也是没有任何意义的。因为这里的命名空间的值sss是任意定义的,我们要使得它看起来有意义,我们一般使用如下的值xmlns:myxmlns=""http://schemas.android.com/apk/res/<你的应用程序的包名>",同时我们还需要在values目录下创建一个attrs.xml的文件,文件的内容看起来像这样的:
Xml代码
  1. <resources>
  2. <declare-styleablename="LabelView">
  3. <attrname="text"format="string"/>
  4. <attrname="textColor"format="color"/>
  5. </declare-styleable>
  6. </resources>

这时如果你使用以下的布局文件IDE就会报错了

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:myxmlns="http://schemas.android.com/apk/res/com.zbkc.custumview"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <TextViewandroid:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. <Viewmyxmlns:sss="sss"/>
  11. </LinearLayout>


他会在 <View myxmlns:sss="ssss" />这一行提示myxmlns的命名空间下sss这个属性,但我们可以使用如下的布局文件而不会报错:
Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:myxmlns="http://schemas.android.com/apk/res/com.zbkc.custumview"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <TextViewandroid:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. <Viewmyxmlns:text="sss"
  11. myxmlns:textColor="#ffffffff"/>
  12. </LinearLayout>


因为text和textColor属性在前面的布局文件中已经被声明。实际上给任何android自带的控件增加自定义属性都是无意义的做法,但对于自定义的View(继承自View的类)来增加自定义的属性却是有很大的实际意义的,如,我们可能经常看到有如下的声明:
Xml代码
  1. <MyViewandroid:layout_width="fill_parent"
  2. android:layout_height="fill_parent"
  3. myxmlns:anyproperty="value"/>

这就是为自定义的View添加了额外的属性,但是刚有了我们前面的声明,并没有什么实际的意义,因为只是做到了声明IDE不报错,并没有任何实际的意义,我们现在要做的就是如何在自定义的View中(注意,只能在自定义的View中来取得这些值)取得我们在布局文件中声明的属性值。
我们下面来写一个稍微完整一点的代码来演示一下完整的过程:
java代码

Java代码
  1. classLabelViewextendsView{
  2. privateStringtext;
  3. privateinttextColor;
  4. publicLabelView(Contextcontext,AttributeSetattrs){
  5. super(context,attrs);
  6. TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.LabelView);
  7. text=(String)a.getText(R.styleable.LabelView_text);
  8. textColor=a.getColor(R.styleable.LabelView_textColor,0xff000000);
  9. Log.i("test","text:"+text);
  10. Log.i("test","textColor:"+textColor);
  11. a.recycle();
  12. }
  13. }


Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:myxmlns="http://schemas.android.com/apk/res/com.zbkc.custumview"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <com.zbkc.custumview.LabelView
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. myxmlns:text="sss"
  11. myxmlns:textColor="#ff00ff00"/>
  12. </LinearLayout>

我们另外再加一个入口代码就可以发现可以取得我们自定义声明的值了
转自:http://hi.baidu.com/xiaofanqing/blog/item/dd9c55fb62256677024f560b.html

更多相关文章

  1. Android的EditText设置光标一直显示而不闪烁的问题
  2. Android电话拨号器
  3. Android(安卓)EditText 属性
  4. 2011.06.21(2)——— android invalidate和postInvalidate
  5. Android中shape属性详解
  6. Activity属性设置大全
  7. android设置横屏代码
  8. 带weight的LinearLayout嵌套RecyclerView导致RecycleView执行多
  9. android 开发中判断网络是否连接的代码

随机推荐

  1. Android 学习小结
  2. android 非系统APP访问字符设备
  3. 将support的包名转换成androidx的包名
  4. android 中SoundPool总结
  5. Android 自定义View自定义属性的声明
  6. android 学习笔记(一):1 环境搭建
  7. android 的webview调用php服务器js , js
  8. Android事件分发机制 详解攻略
  9. Android控件布局常用属性
  10. Android Telephony框架结构简析