xml命名空间如何为自定义View取名?

2016-05-08 00:59 阅读 6,953 次 评论 0 条

一.摘要

xml布局文件的开头经常自动生成——xml:android="scheme.android.com/apk/res/android",这一段代码,然后在父控件中添加的所有子控件中都可以使用——android:layout_width=""类似的代码,“android:”被称为命名空间,创建xml布局时,默认使用Google自定义的命名空间,即使用其所有封装好的自定义View,比如:TextView,Button,EditText等,命名空间后面是引用自定义View对应的属性,并且为属性赋值,比如:android:layout_width="wrap_content",android:layout_centerInParent="true"等,layout_width,layout_centerInParent是定义在attrs.xml文件中的属性;wrap_content,true是给对应属性设置的值。
beautiful_girl

二.设置xmlns

设置xmlns的方式有两种:

方式一:

  1. xmlns:app="http://schemas.android.com/apk/res-auto"  

方式二:

  1. xmlns:app="http://schemas.android.com/apk/res/包名"  

app的名字可以任意取(只要不冲突),然后按照上述方式,在自定义的View中给属性赋值,例如:

  1. <cn.teachcourse.intent.utils.CircleImageView xmlns:circle="http://schemas.android.com/apk/res/cn.teachcourse"  
  2.         android:id="@+id/set_circle_img"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_centerInParent="true"  
  6.         circle:border_width="2dp"  
  7.         circle:border_color="@color/colorAccent" />  

你可能感兴趣的文章

来源:TeachCourse每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1725.html ,谢谢支持!

资源分享

分类:Android 标签:,
Android开发之UML类图简介 Android开发之UML类图简介
windows系统自动化批处理命令 windows系统自动化批处理命令
浅谈短信服务SMS 浅谈短信服务SMS
Android事件分发流程分析证明(1) Android事件分发流程分析证明(