* **命名**?遵循前綴表明類型的習慣,形如`type_foo_bar.xml`。例如:`fragment_contact_details.xml`,`view_primary_button.xml`,`activity_main.xml`.
**組織布局文件**?若果你不確定如何排版一個布局文件,遵循一下規則可能會有幫助。
* 每一個屬性一行,縮進4個空格
* `android:id`?總是作為第一個屬性
* `android:layout_****`?屬性在上邊
* `style`?屬性在底部
* 關閉標簽`/>`單獨起一行,有助于調整和添加新的屬性
* 考慮使用[Designtime attributes 設計時布局屬性](http://tools.android.com/tips/layout-designtime-attributes),Android Studio已經提供支持,而不是硬編碼`android:text`?(譯者注:墻內也可以參考stormzhang的這篇博客[鏈接](http://stormzhang.com/devtools/2015/01/11/android-studio-tips1/))。
~~~
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/name"
style="@style/FancyText"
/>
<include layout="@layout/reusable_part" />
</LinearLayout>
~~~
作為一個經驗法則,`android:layout_****`屬性應該在 layout XML 中定義,同時其它屬性`android:****`?應放在 styler XML中。此規則也有例外,不過大體工作 的很好。這個思想整體是保持layout屬性(positioning, margin, sizing) 和content屬性在布局文件中,同時將所有的外觀細節屬性(colors, padding, font)放 在style文件中。
例外有以下這些:
* `android:id`?明顯應該在layout文件中
* layout文件中`android:orientation`對于一個`LinearLayout`布局通常更有意義
* `android:text`?由于是定義內容,應該放在layout文件中
* 有時候將`android:layout_width`?和?`android:layout_height`屬性放到一個style中作為一個通用的風格中更有意義,但是默認情況下這些應該放到layout文件中。
**使用styles**?幾乎每個項目都需要適當的使用style文件,因為對于一個視圖來說有一個重復的外觀是很常見的。 在應用中對于大多數文本內容,最起碼你應該有一個通用的style文件,例如:
~~~
<style name="ContentText">
<item name="android:textSize">@dimen/font_normal</item>
<item name="android:textColor">@color/basic_black</item>
</style>
~~~
應用到TextView 中:
~~~
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/price"
style="@style/ContentText"
/>
~~~
你或許需要為按鈕控件做同樣的事情,不要停止在那里。將一組相關的和重復`android:****`的屬性放到一個通用的style中。
**將一個大的style文件分割成多個文件**?你可以有多個`styles.xml`?文件。Android SDK支持其它文件,`styles`這個文件名稱并沒有作用,起作用的是在文件 里xml的``標簽。因此你可以有多個style文件`styles.xml`,`style_home.xml`,`style_item_details.xml`,`styles_forms.xml`。 不用于資源文件路徑需要為系統構建起的有意義,在`res/values`目錄下的文件可以任意命名。
**`colors.xml`是一個調色板**?在你的`colors.xml`文件中應該只是映射顏色的名稱一個RGBA值,而沒有其它的。不要使用它為不同的按鈕來定義RGBA值。
_不要這樣做_
~~~
<resources>
<color name="button_foreground">#FFFFFF</color>
<color name="button_background">#2A91BD</color>
<color name="comment_background_inactive">#5F5F5F</color>
<color name="comment_background_active">#939393</color>
<color name="comment_foreground">#FFFFFF</color>
<color name="comment_foreground_important">#FF9D2F</color>
...
<color name="comment_shadow">#323232</color>
~~~
使用這種格式,你會非常容易的開始重復定義RGBA值,這使如果需要改變基本色變的很復雜。同時,這些定義是跟一些環境關聯起來的,如`button`或者`comment`, 應該放到一個按鈕風格中,而不是在`color.xml`文件中。
相反,這樣做:
~~~
<resources>
<!-- grayscale -->
<color name="white" >#FFFFFF</color>
<color name="gray_light">#DBDBDB</color>
<color name="gray" >#939393</color>
<color name="gray_dark" >#5F5F5F</color>
<color name="black" >#323232</color>
<!-- basic colors -->
<color name="green">#27D34D</color>
<color name="blue">#2A91BD</color>
<color name="orange">#FF9D2F</color>
<color name="red">#FF432F</color>
</resources>
~~~
向應用設計者那里要這個調色板,名稱不需要跟"green", "blue", 等等相同。 "brand_primary", "brand_secondary", "brand_negative" 這樣的名字也是完全可以接受的。 像這樣規范的顏色很容易修改或重構,會使應用一共使用了多少種不同的顏色變得非常清晰。 通常一個具有審美價值的UI來說,減少使用顏色的種類是非常重要的。
**像對待colors.xml一樣對待dimens.xml文件**?與定義顏色調色板一樣,你同時也應該定義一個空隙間隔和字體大小的“調色板”。 一個好的例子,如下所示:
~~~
<resources>
<!-- font sizes -->
<dimen name="font_larger">22sp</dimen>
<dimen name="font_large">18sp</dimen>
<dimen name="font_normal">15sp</dimen>
<dimen name="font_small">12sp</dimen>
<!-- typical spacing between two views -->
<dimen name="spacing_huge">40dp</dimen>
<dimen name="spacing_large">24dp</dimen>
<dimen name="spacing_normal">14dp</dimen>
<dimen name="spacing_small">10dp</dimen>
<dimen name="spacing_tiny">4dp</dimen>
<!-- typical sizes of views -->
<dimen name="button_height_tall">60dp</dimen>
<dimen name="button_height_normal">40dp</dimen>
<dimen name="button_height_short">32dp</dimen>
</resources>
~~~
布局時在寫 margins 和 paddings 時,你應該使用`spacing_****`尺寸格式來布局,而不是像對待String字符串一樣直接寫值。 這樣寫會非常有感覺,會使組織和改變風格或布局是非常容易。
**避免深層次的視圖結構**?有時候為了擺放一個視圖,你可能嘗試添加另一個LinearLayout。你可能使用這種方法解決:
~~~
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
...
>
<LinearLayout
...
>
<LinearLayout
...
>
<LinearLayout
...
>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
~~~
即使你沒有非常明確的在一個layout布局文件中這樣使用,如果你在Java文件中從一個view inflate(這個inflate翻譯不過去,大家理解就行) 到其他views當中,也是可能會發生的。
可能會導致一系列的問題。你可能會遇到性能問題,因為處理起需要處理一個復雜的UI樹結構。 還可能會導致以下更嚴重的問題[StackOverflowError](http://stackoverflow.com/questions/2762924/java-lang-stackoverflow-error-suspected-too-many-views).
因此盡量保持你的視圖tree:學習如何使用[RelativeLayout](https://developer.android.com/guide/topics/ui/layout/relative.html), 如何?[optimize 你的布局](http://developer.android.com/training/improving-layouts/optimizing-layout.html)?和如何使用?[``?標簽](http://stackoverflow.com/questions/8834898/what-is-the-purpose-of-androids-merge-tag-in-xml-layouts).
**小心關于WebViews的問題.**?如果你必須顯示一個web視圖, 比如說對于一個新聞文章,避免做客戶端處理HTML的工作, 最好讓后端工程師協助,讓他返回一個 "_純_" HTML。?[WebViews 也能導致內存泄露](http://stackoverflow.com/questions/3130654/memory-leak-in-webview)?當保持引他們的Activity,而不是被綁定到ApplicationContext中的時候。 當使用簡單的文字或按鈕時,避免使用WebView,這時使用TextView或Buttons更好。