在Android UI開發中,咱們能夠經過IDE看到xml佈局的預覽效果,可是有些控件只有在運行後才能顯示,好比TextView,咱們只有在運行後纔會填充數據,可是有些時候咱們須要提早預覽效果,便會常常性的寫一些測試數據。好比:TextView的android:text=「滾犢子」。在開發完成後你若是記得把這個數據刪掉還好,若是忘記了,你懂得....,還有就是xml中有一些警告,固然這些警告並不影響編譯,可是對於一些有強迫症的人來講,仍是頗有殺傷力的。好了,說了這麼多廢話,只爲能引出今天的主題:android tools android
下面咱們來看下Android tools的做用和使用方法 程序員
首先在使用的時候咱們須要在xml中添加: api
xmlns:tools="http://schemas.android.com/tools" 佈局
tools能夠告訴咱們的編譯器,那些屬性是針對佈局設計的,在運行時是要被忽略的。tools能夠覆蓋Android的所有標準屬性,把Android:換成tools:便可。運行時便會連同tools屬性一塊兒忽略掉,不會出如今apk中。好比: 測試
<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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="滾犢子"/>
</LinearLayout> spa
這樣你就不用擔憂你會忘記刪除測試數據,能夠安心發版了。 設計
tools的屬性可分爲兩種,一種是咱們上面說的,覆蓋Android標準屬性,關於xml佈局設計的。還有一種是關於lint提示的。 code
上面咱們說的是佈局設計的做用和用法,下面說關於Lint的用法 xml
Lint相關的屬性主要有三個: ip
tools:ignore=""
tools:targetApi=""
tools:locale=""
一、tools:ignore
這個屬性的主要做用即是忽略xml中的某些警告,你好比咱們在一個ImageView中,咱們並不須要設置android:contentDescription屬性,可是當你的ImageView沒有設置這個屬性時,警告便會出來噁心你,他不會影響的編譯,只是會出來噁心你而已。使用方式:
<ImageView
android:id="@+id/navigation_item_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="contentDescription"
/>
二、tools:targetApi
這個屬性主要是消除你使用了比你設置最低SDK版本高的控件時的警告,例如:當你設置的最低SDK版本:minSdkVersion=8,而你使用了api21的控件,此時便會有出現警告。使用方式爲:
<FrameLayout
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:targetApi="LOLLIPOP"
>
</FrameLayout>
三、tools:locale
這個屬性做用在res/value/string.xml下,默認狀況下res/values/strings.xml中的字符串會執行拼寫檢查,若是不是英語,會提示拼寫錯誤,經過如下代碼來告訴studio本地語言不是英語,就不會有提示了。使用方式:
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:locale="it"
>
</resources>
Lint的這些屬性不瞭解也沒問題的,則個屬性就是爲那些潔癖程序員設置的,由於即便你不去管它也不會影響你程序的運行。
這篇文章就到這裏,後面咱們會介紹Android:tools的非標準UI設計預覽的屬性。