你是不是也忽略了xml裏面的tools命名空間

一創建一個佈局文件的時候可能都有這麼一句:

xmlns:tools="http://schemas.android.com/tools"

基礎認識

可是這個tools的命名空間有什麼屬性有什麼作用呢,相信很多人都忽略了它的存在。它主要用在項目開發階段而不會影響用戶體驗,用在Design界面渲染而不會影響運行時的界面。

有時這些巧妙的屬性會節約我們的構建時間。我並不是說會加快構建速度,而是構建相關的 UI 改變會減少。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

當然這個tools你可以改成其它,只要他能代表這個命名空間就可以。該屬性的所有屬性都不會影響apk的大小或運行時,它們會在Gradle打包應用時被分離出去。
以前在做andorid開發時,爲了直觀的查看佈局經常在TextView裏面用android:text=”“來添加測試字符串,然而這樣做就會讓我們apk中包含沒有用的資源和渲染成本。有這個tools我們可以用tools:text=”“,這樣就會在設計渲染時顯示出來而不在運行時顯示。
我們可以同時使用android和tools命名空間。tools命名空間將會用在設計階段而前者會用在運行時。
有時候你希望在運行時開啓而有設計時關閉。Android文檔展示了ListView的例子:

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fastScrollAlwaysVisible="true"
    tools:fastScrollAlwaysVisible=""/>

這裏表示:有運行時開啓了fastScrollAlwaysVisible 功能,而在設計時關閉了它。
你可以覆蓋所有已存在的android命名空間的屬性,但無法覆蓋自定義屬性。

在xml中指定目標API版本

你可以在xml中指定API版本,就像在java代碼中使用@TargetApi一樣。API版本可以通過一個整形或它的代號指定。這可以用來避免特定XML屬性的問題。比如:

<TextView
    tools:text="Mastering ToolsNs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_height="match_parent"
    tools:targetApi="M"
    />

告訴Lint你的字符串資源是正確的

由於AndroidStudio/Lint默認語言是英語,如果你有其它語言的字符串資源,它將會顯示排版警告。
告知Lint你本地化資源的技巧:

<resources xmlns:tools="http://schemas.android.com/tools" 
    tools:locale="es"/>

這樣做了之後就不會顯示排版警告了。

在Fragment和自定義視圖上預覽佈局

在使用Fragment和自定義視圖時非常有用。通過tools:layout="@layout/your_layout",你可以設置在預覽窗口顯示一個佈局。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >

    <fragment
        android:name="com.alexsimo.mastertoolsnamespace.BooksFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_books"
        />

</LinearLayout>

上述代碼使用了 tools:layout 屬性來預覽 BooksFragment 佈局,而不用將工程運行在設備或模擬器上來查看效果。是不是覺得很方便呢?

關聯xml到activity上

當我們用AndroidStudio創建一個acitivty的時候,在默認生成的xml文件中你會看到tools:context=".MainActivity"。單個xml佈局可以用在多個activity或fragment中,使用些屬性,你就告訴了AndroidStudio哪個activity與這個xml相關聯。
當主題被定義在了AndroidManifest.xml文件之後這個幫助佈局編輯器猜測這個activity的主題。

忽略LInt警告

忽略Lint警告不是一個好主意,如果Lint上報問題,你應該行動起來並修復錯誤和警告,但有時Lint給出的是錯誤警告,我們明確知道(may be not)我們在做什麼,這種情況下你可以用使用tool:ignore=""
如果我們的外層佈局沒有存在的意義的時候:

 <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent" >

        <Button
            android:id="@+id/dummy_button"
            style="?buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dummy_button" />
   </LinearLayout>

可以用tools:ignore="UselessParent" 來忽略警告。

帶菜單預覽佈局

默認情況下,定義在 Activity.onCreateOptionsMenu() 中的菜單會被渲染到預覽窗口。但你可以使用 tools:menu=”comma separated menu IDs” 覆蓋此菜單。我個人不會使用該屬性,但它可能會對你有用。

設置 Toolbar 導航模式

此屬性很簡單!使用 tools:actionBarNavMode=”standard|list|tabs” 你可以設置 ActivityBar 的導航模式。

Shrink resources(收縮資源)

Android tools 命名空間中有許多關於收縮資源的屬性,比如 tools:shrinkMode=”strict|safe”,tools:keep=”@layout|layout_wildcard”,tools:discard=”@layout/unused” 等,但我不準備在此討論它們,因爲本文不是討論收縮資源的,如果你感興趣,可以在 Android Studio 官方文檔中瞭解更多信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章