Android Tools Attributes,讓布局設計所見即所得

KamiStarke 8年前發布 | 7K 次閱讀 安卓開發 Android開發 移動開發

開發人員在設計Android Layout布局時,總會伴隨著一些亂七八槽的困擾。比如,為了更加逼真的真實數據預覽效果,我們在開發時會將TextView的text屬性寫上一些假數據,而當運行到模擬器或真機上時這些假數據就成了造成體驗上甚至測試BUG的臟數據,又需要一一清除。再比如,我們想在XML的預覽界面上就看到ListView的Item內容,而不是只有通過編譯運行時才能查看。等等,諸如這些存在于開發Layout內容階段的困擾,都可以通過Tools Attributes得以解決,不妨了解一下。

xmlns 定義

Android提供了一種特殊的tools命名空間,來幫助開發人員在開發Layout布局時使用tools屬性解決實時預覽等問題。這種屬性在應用打包運行時將會被忽略,不產生任何影響。tools命名空間的URI為: http://schemas.android.com/tools ,通常可將其定義在Layout的根容器中(在Android Studio中,可利用Live Temples快捷方式,輸入toolsNs即可對應提示),如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

</RelativeLayout>

這里將Tools Attributes按照功能分為兩種類別,一種是去除Lint提示的,一種是展示布局預覽的,下面一一介紹相關屬性的使用。

Lint 提示

tools:ignore

這個屬性用于告訴 Android Lint 忽視某些xml警告,比如平時使用ImageView的時候都要加上這么一句: android:contentDescription="@null" ,否則便會出現黃色警告:[Accessibility] Missing contentDescription attribute on image,此時就可以使用 tools:ignore 屬性忽視這個警告:

<ImageView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:ignore="contentDescription"/>

tools:targetApi

類似Java代碼中的 @TargetApi 注解一樣,指定API級別,可以使用數字level,也可以直接用API name,比如elevation屬性只能在API 21及更高版本使用,使用 tools:targetApi 屬性可以忽視版本的警告:

<ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 tools:targetApi="LOLLIPOP"/>

tools:locale

指定IDE當前的假定本地語言,可用在資源文件的根元素上,比如用在 values/strings.xml 上,避免一些拼寫語法上的檢查:

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

    <!-- string -->
    <string name="name">nombre</string>

</resources>

布局預覽

這里也分為兩種,一種是替換標準的android命名空間的控件固有屬性,舉個例子:

<TextView
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 tools:text="Samples"/>

上例中使用tools:text替換了android:text標準屬性,在design窗口預覽時可以看到TextView的text內容,而運行時則會被忽略。諸如此類,其他android標準屬性均可被替換以達到預覽效果。另一種就是非android標準屬性了,下面一一說明。

tools:context

這個屬性用在layout文件的根元素上,指明與當前layout相關聯的Activity,從而在預覽時使用Activity的主題(theme一般定義在Manifest文件中并且與activities聯系在一起,而非layout)。可以使用Activity的全名,也可以利用manifest中定義的包名作為前綴:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 tools:context=".MainActivity"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

</RelativeLayout>

tools:layout

這個屬性主要用于 標簽中,指定預覽時用的layout布局文件內容:

<fragment
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:layout="@layout/fragment_content"/>

tools:listitem/listheader/listfooter

顧名思義,這三個屬性可用于諸如ListView、GridView、ExpandableListView等AdapterView的子類中,實現內容布局的預覽。注意:經實踐,在Android Studio中已經無法達到這些元素的內容預覽效果,但 tools:listitem 屬性可以用在 RecyclerView 中,比如:

<android.support.v7.widget.RecyclerView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:listitem="@android:layout/simple_list_item_checked"/>

RecyclerView使用tools:listitem的前后對比預覽效果如下:

tools:showIn

這個屬性用在 標簽所包含的layout的根元素上,指定一個 所在的布局文件,這樣在被嵌套的layout的design視圖中便可以預覽到外層的layout內容。比如在activity_main.xml中使用 <include> 標簽嵌套一個include_content.xml文件,那么在include_content.xml文件的根元素中就可以使用 tools:showIn 屬性指定Outer layout,達到在include_content.xml文件中預覽activity_main.xml內容的效果:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 tools:showIn="@layout/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

</RelativeLayout>

tools:menu

這個屬性用在layout根元素中,指定layout預覽時ActionBar展示的menu內容。使用 tools:context 屬性時,ActionBar會自動查找Activity中的 onCreateOptionsMenu() 方法預覽menu, tools:menu 屬性的設置會覆蓋 tools:context 屬性的效果。 tools:menu 的值只需要使用menu文件的名字即可,多個menu使用逗號隔開,如

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 tools:menu="menu1,menu2"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

</RelativeLayout>

tools:actionBarNavMode

這個屬性用在layout根元素中,指定layout預覽時ActonBar的導航模式,值有standard、list和tabs三種,如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 tools:actionBarNavMode="standard"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

</RelativeLayout>

ActionBar不同navigation mode下的預覽效果如圖所示,關于navigation mode的相關信息可參考 Top Level Switching With View Controls

tools:shrinkMode/keep/discard

為了使APK文件盡可能地變小,一般在打包發布時會開啟Shrink Code和Shrink Resources的功能,刪除項目中無用的代碼和資源,使用這三個屬性可以指定某些resources的保留與刪除。

 

來自:http://yifeng.studio/2016/10/13/android-tools-attributes/

 

 本文由用戶 KamiStarke 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!