【UI篇】Android布局理解之xmlns

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>

細(xì)節(jié)無處不在,學(xué)習(xí)還是要想對(duì)待這個(gè)命名空間一樣去追根究底渠啊。繼續(xù)努力输吏。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市替蛉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拄氯,老刑警劉巖躲查,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異译柏,居然都是意外死亡镣煮,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門鄙麦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來典唇,“玉大人镊折,你說我怎么就攤上這事〗橄危” “怎么了恨胚?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)炎咖。 經(jīng)常有香客問我赃泡,道長(zhǎng),這世上最難降的妖魔是什么乘盼? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任升熊,我火速辦了婚禮,結(jié)果婚禮上绸栅,老公的妹妹穿的比我還像新娘级野。我一直安慰自己,他們只是感情好粹胯,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布勺阐。 她就那樣靜靜地躺著,像睡著了一般矛双。 火紅的嫁衣襯著肌膚如雪渊抽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天议忽,我揣著相機(jī)與錄音懒闷,去河邊找鬼。 笑死栈幸,一個(gè)胖子當(dāng)著我的面吹牛愤估,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播速址,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼玩焰,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了芍锚?” 一聲冷哼從身側(cè)響起昔园,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎并炮,沒想到半個(gè)月后默刚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡逃魄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年荤西,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡邪锌,死狀恐怖勉躺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情觅丰,我是刑警寧澤饵溅,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站舶胀,受9級(jí)特大地震影響概说,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嚣伐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一糖赔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧轩端,春花似錦放典、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至拱层,卻和暖如春弥臼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背根灯。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工径缅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人烙肺。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓纳猪,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親桃笙。 傳聞我的和親對(duì)象是個(gè)殘疾皇子氏堤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容