XML命名空间提供避免元素命名冲突的方法。
打个比方,A学校有名同学叫小明,B学校也有名同学叫小明,那我们如何识别这两名拥有相同名字的同学呢?这时候命名空间就派上用场了。A和B此时就可以被当成是命名空间了。也就是说,命名空间里面存放的是特定属性的集合。
Android中常见的命名空间
下面分别介绍android、tools、app(自定义命名空间)这几个常见命名空间
1、android
xmlns:android=”http://schemas.android.com/apk/res/android”
在Android布局文件中我们都必须在根元素上定义这样一个命名空间,接下来对这行代码进行逐一讲解:
xmlns:即xml namespace,声明我们要开始定义一个命名空间了;
android:称作namespace-prefix,它是命名空间的名字;
http://schemas.android.com/apk/res/android:这看起来是一个URL,但是这个地址是不可访问的,实际上这是一个URI,所以它的值是固定不变的,相当于一个常量。
有了他,就会提示你输入什么,也可以理解为语法文件。
使用这行代码,我们就可以引用命名空间中的属性,如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
在这个布局中,只要以android:开头的属性便是引用了命名空间中的属性,android是赋予命名空间一个名字,就跟我们平时在定义变量一样,比如我把它取成myns,那么上面的代码也可以写成:
<LinearLayout xmlns:myns="http://schemas.android.com/apk/res/android"
myns:layout_width="match_parent"
myns:layout_height="match_parent" >
<TextView
myns:layout_width="wrap_content"
myns:layout_height="wrap_content"
myns:layout_gravity="center"
myns:text="New Text"
myns:id="@+id/textView" />
</LinearLayout>
2、tools
xmlns:tools=”http://schemas.android.com/tools”
接下来会介绍,关于tools的三种使用方法。
2.1、tools只作用于开发阶段
我们可以把它理解为一个工具(tools)的命名空间,它只作用于开发阶段,当app被打包时,所有关于tools的属性将都会被摒弃掉。
例如,基本上在android命名空间内的属性,我们想在编写代码阶段测试某个组件在屏幕上的效果,而当app安装到手机上时,摒弃掉这条代码,那么我们就可以用tools命名空间来代替掉android:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
tools:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello,World"/>
</LinearLayout>
以上是在layout中的布局,当我们切换到视图窗口中查看时,看到的是标签顶部居中显示:
然后,当我们运行到手机上时,却是这样的:
如上所示,tools:layoutgravity= “center”确实在运行后被抛弃掉了!
2.2、tools:context开发中查看Activity布局效果
context的用法是在后面跟一个Activity的完整包名,当我们设置一个Activity的主题时,是在AndroidManifest.xml中设置的,而主题的效果又只能运行后在Activity中显示,使用context属性,就可以在开发阶段中看到设置在Activity中的主题效果。
tools:context=”com.littlehan.myapplication.MainActivity”
在布局中加入这行代码,就可以在开发阶段中看到设置在Activity中的主题效果。
2.3、tools:layout开发中查看fragment布局效果
当我们在Activity上加载一个fragment时,是需要在运行后才可以看到加载后的效果的,借助layout属性,就可以在运行的时候就看到加载后的效果。
tools:layout=@layout/yourfragmentlayoutname
这样你的编写的fragment布局就会预览在指定主布局上了。
3、自定义命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"
当Android自带的控件不能满足需求时,可以自己去绘制一些View,而要为自定义View加上自定义的属性时,就需要创建自定义命名空间。
命名空间里存放的是特定属性的集合,这样一来,思路就很清晰,也就是说自定义命名空间的过程实际上就是自定义属性。
3.1 自定义View
public class CustomTextView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//画笔
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs){
this(context, attrs, 0);//注意不是super(context,attrs,0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr){
super(context,attrs,defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("I am a CustomTextView",100, 100, mPaint);
}
}
3.2、自定义属性
在values根目录下新建一个名为attrs的xml文件来自定义属性(自定义的属性便是自定义命名空间里面的属性)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTitleView">
<attr name="attribute1" format="string" />
<attr name="attribute2" format="color" />
<attr name="attribute3" format="dimension" />
</declare-styleable>
</resources>
name定义的是属性的名字,format定义的是属性的类型。
3.3、使用自定义布局
如果要使用自定义属性,就需要自定义属性命名空间,在布局文件的根元素下插入这样一行代码:
xmlns:app=”http://schemas.android.com/apk/res-auto”
就可以使用自定义属性了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.customview.CustomTitleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
app:attribute1="@string/text"
app:attribute2="#ff0000"
app:attribute3="40sp" />
</LinearLayout>