1、什么是xmlns?有什么用洲尊?
xmlns,即xml 命名空間。其作用是區(qū)分不同來源和用途的屬性奈偏,以免產(chǎn)生多個(gè)屬性同名無法區(qū)分的錯(cuò)誤坞嘀。
2、常用的三個(gè)xmlns?
常用的三個(gè)xmlns惊来。
一丽涩、xmlns:android="http://schemas.android.com/apk/res/android"
它是在Android布局文件xml中在根布局中聲明使用的。例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.CustomView.CustomViewActivity"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rela_cus">
</RelativeLayout>
其作用是支持使用并提示android 原生控件裁蚁、常用布局自帶的屬性矢渊,所有例如:
android:layout_height="match_parent"
像這樣帶著android前綴的屬性值都是android命名空間的屬性。
其作用就是原生View屬性的語(yǔ)法文件枉证。
二矮男、xmlns:tools="http://schemas.android.com/tools"
他的作用有以下三點(diǎn)。
Point 1
他僅僅作用于開發(fā)調(diào)試階段室谚,當(dāng)打包為apk的時(shí)候所有tools添加的屬性都會(huì)被摒棄毡鉴。仔細(xì)觀察,這個(gè)URI在*.com/后面并沒有apk/這個(gè)部分秒赤,也算是體現(xiàn)了這個(gè)特點(diǎn)猪瞬。這個(gè)可以用來在寫頁(yè)面的時(shí)候調(diào)試,而且很多android空間中有的屬性入篮,tools空間中都有陈瘦。所以這樣就方便進(jìn)行頁(yè)面的調(diào)整。即使忘記刪除也不會(huì)影響到最終的結(jié)果崎弃。
Point 2
tools:context="XXActivity的包名路徑"
這個(gè)的作用是直接在編寫xml文件階段就能看到包含當(dāng)前Activity的主題限制在內(nèi)的頁(yè)面樣式
Point 3
tools:layout=@layout/your fragment layout name
這個(gè)的作用是直接在編寫xml文件階段就能看到包含目標(biāo)Fragment所在的頁(yè)面樣式甘晤。
三含潘、xmlns:app="http://schemas.android.com/apk/res-auto"
這個(gè)命名空間是結(jié)合自定義控件來使用的。在下一個(gè)部分我們會(huì)分析自定義xmlns和這個(gè)的等價(jià)關(guān)系线婚。
3遏弱、如何自定義xmlns?
首先我們明確一下,自定義xmlns的目的是開辟一個(gè)空間來為自定義View定義一些屬性值塞弊,而且為了讓我們明確區(qū)分開這個(gè)屬性是自定義View自身特有的漱逸,我們可以自定義一個(gè)與之對(duì)應(yīng)的xmlns來設(shè)置這個(gè)屬性。
命名方法是:xmlns:自定義變量名="http://schemas.android.com/apk/自定義View的包名路徑"游沿。例:
xmlns:myViewns="http://schemas.android.com/apk/自定義View的包名路徑"
當(dāng)使用其屬性時(shí)饰抒,其格式為:
myViewns:text="我是修改版!"
myViewns:textColor="#000000"
但是在Android Studio 2.0以上就不推薦這么寫诀黍。最好還是使用官方定義好的自定義View的自適應(yīng)空間袋坑。
xmlns:app="http://schemas.android.com/apk/res-auto"
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.CustomView.CustomViewActivity"
android:id="@+id/rela_cus">
<com.lay.demopro74.ui.CustomView.MyTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:text="999"
app:textColor="#000000"
/>
</RelativeLayout>
這個(gè)東西的使用是需要自定義控件配合的。
Step 1
在values文件夾下眯勾,新建一個(gè)attrs資源文件枣宫,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="text" format="string"></attr>
<attr name="textColor" format="color"></attr>
</declare-styleable>
</resources>
Step 2
在自定義文件的構(gòu)造方法中解析上下文對(duì)應(yīng)的資源文件內(nèi)容,得到xml中設(shè)置的屬性值吃环。
package com.lay.demopro74.ui.CustomView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import com.lay.demopro74.R;
/**
* User: AnyiLiu
* Date: 2019/9/24
* Time: 14:05
*/
public class MyTextView extends View {
private Paint mPaint;
private String mTextValue="我是默認(rèn)版也颤!";
private int mColor=Color.RED;
public MyTextView(Context context) {
this(context,null);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
CharSequence mText = typedArray.getText(R.styleable.MyTextView_text);
if(!TextUtils.isEmpty(mText)){
mTextValue=mText.toString();
}
mColor = typedArray.getColor(R.styleable.MyTextView_textColor, Color.rgb(201, 231, 180));
// mPaint.setColor(mColor);
typedArray.recycle();
initPaint();
}
//畫筆
private void initPaint() {
mPaint=new Paint();
mPaint.setColor(mColor);
// mPaint.setAntiAlias(true);
// mPaint.setDither(true);
mPaint.setStrokeWidth(40f);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mTextValue,100,100,mPaint);
}
//修改內(nèi)容、位置和顏色-方案一:View類中寫set方法郁轻;
//xml-修改內(nèi)容翅娶、位置、顏色
}
Step 3
最后才在xml文件中直接使用好唯。這也是為什么很多第三方控件的自定義屬性都是app空間格式的原因竭沫。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.CustomView.CustomViewActivity"
android:id="@+id/rela_cus">
<com.lay.demopro74.ui.CustomView.MyTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:text="999"
app:textColor="#000000"
/>
</RelativeLayout>