Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android XML 佈局實用屬性與技巧大整理

編輯:關於 Android 程式設計

寫 Android 介面時,真正花時間的往往不是邏輯,而是那些零散的佈局屬性。這篇把日常最常用到的屬性、樣式與自訂元件寫法整理成一份可隨時翻查的清單。

重新整理與可見性

postInvalidate();                         在子執行緒中請求重繪介面
invalidate();                             在當前執行緒中請求重繪

android:visibility="visible"              顯示
android:visibility="invisible"            佔位但不顯示
android:visibility="gone"                 連佔位空間一起消失

這三者的差別很實際:invisible 的元件還會佔著版面,gone 則完全讓位給其他元件。

背景與對齊

android:background="@null"                移除元件背景
android:layout_gravity="right|center_vertical"   靠右並垂直居中
android:gravity="center_horizontal"       元件「內容」水平居中

gravity 與 layout_gravity 經常被搞混:前者決定元件「內部內容」在元件裡的位置(例如按鈕文字靠左或靠右),後者決定這個元件「本身」在父容器裡的位置。

Button 與圖示

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/star"
    android:text="按鈕1" />

drawableTop 把圖示放在文字上方,同理還有 drawableBottom、drawableLeft、drawableRight。

<Button android:layout_width="120dip"
    android:layout_height="wrap_content"
    android:text="切換程序"
    style="@android:style/ButtonBar"
    android:id="@+id/btn7"
    android:layout_weight="1" />

style="@android:style/ButtonBar" 讓單顆按鈕呈現「按鈕條」的外觀,搭配 layout_weight 就能做出一排等寬的切換鈕。

引入其他佈局與 SlidingDrawer

<include
    android:id="@+id/vip_include_top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    layout="@layout/top_1" />

<include> 讓你把常用區塊(標題列、底部列)抽成獨立 layout 重複使用,避免整份 XML 越寫越長。

<SlidingDrawer
    android:id="@+id/sliding"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:content="@+id/allApps"
    android:handle="@+id/imageViewIcon"
    android:orientation="vertical" >

抽屜式設計一定要指定 content(抽屜裡的內容元件)與 handle(使用者拖曳的把手元件),少了任何一個都跑不起來。

隱藏標題與全螢幕

requestWindowFeature(Window.FEATURE_NO_TITLE);   隱藏單一 Activity 的標題列

this.getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);  全螢幕

想一次去掉所有 Activity 的標題列,可以在 AndroidManifest.xml 的 <application> 標籤上加上 android:theme="@android:style/Theme.NoTitleBar"。

自訂按鈕樣式

<resources>
    <style name="tab_btn">
        <item name="android:textAppearance">@style/tab_item_text_style</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">@drawable/trans</item>
        <item name="android:layout_width">0.0dip</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:layout_weight">1.0</item>
    </style>
</resources>

把重複的屬性收進 style,XML 立刻乾淨,之後要調整風格也只需要改一處。

selector:處理焦點與點擊狀態

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/input_normal" />
    <item android:state_pressed="true" android:drawable="@drawable/input_normal" />
    <item android:state_focused="true" android:drawable="@drawable/input_over" />
</selector>

用 selector 描述元件在「停用、按下、取得焦點」等狀態下的外觀,比在程式碼裡手動換背景乾淨得多。

shape:漸層背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#FFFFFF"
        android:endColor="#000000"
        android:angle="270" />
</shape>

存成 res/drawable/bg.xml 後,在程式碼中呼叫 setBackgroundResource(R.drawable.bg) 即可套用。

Gallery 樣式

// 在 BaseAdapter 的 getView 中套用 Gallery 內建樣式
TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
imageView.setBackgroundResource(
        typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
return imageView;

另外 android:spacing="10dp" 可以控制 Gallery 項目之間的間距。

RelativeLayout 屬性分三大類

第一類:布林值,相對於父容器

android:layout_centerHorizontal         水平居中
android:layout_centerVertical           垂直居中
android:layout_centerInParent           相對於父元素完全居中
android:layout_alignParentTop           貼齊父元素上邊緣
android:layout_alignParentBottom        貼齊父元素下邊緣
android:layout_alignParentLeft          貼齊父元素左邊緣
android:layout_alignParentRight         貼齊父元素右邊緣
android:layout_alignWithParentIfMissing 對應的兄弟找不到時,改以父元素為參照

第二類:值為 id 參照,相對於其他元件

android:layout_above="@id/xxx"          在某元件上方
android:layout_below="@id/xxx"          在某元件下方
android:layout_toLeftOf="@id/xxx"       在某元件左邊
android:layout_toRightOf="@id/xxx"      在某元件右邊
android:layout_alignTop="@id/xxx"       上邊緣與某元件對齊
android:layout_alignBottom="@id/xxx"    下邊緣與某元件對齊
android:layout_alignLeft="@id/xxx"      左邊緣與某元件對齊
android:layout_alignRight="@id/xxx"     右邊緣與某元件對齊

第三類:具體間距數值

android:layout_marginTop="10dip"        距某元素上邊緣的距離
android:layout_marginBottom="10dip"     距某元素下邊緣的距離
android:layout_marginLeft="10dip"       距某元素左邊緣的距離
android:layout_marginRight="10dip"      距某元素右邊緣的距離

ImageView 的 scaleType 怎麼選

android:scaleType 決定圖片如何縮放與移動以配合 ImageView 的尺寸,選錯就會出現留白過多或影像被裁掉的窘境。

CENTER / center:維持原始尺寸居中顯示;若圖片長寬超過 View,則只顯示中央部分。

CENTER_CROP / centerCrop:按比例放大圖片並居中,讓圖片的長(寬)等於或大於 View 的長(寬),適合需要填滿不留白的情境。

CENTER_INSIDE / centerInside:完整顯示圖片內容並居中,透過縮小讓長寬等於或小於 View。

FIT_CENTER / fitCenter:按比例縮放到貼合 View 寬度,居中顯示。

FIT_START / fitStart:同上,但靠 View 上方對齊。

FIT_END / fitEnd:同上,但靠 View 下方對齊。

FIT_XY / fitXY:不按比例,直接拉伸填滿 View 大小(可能變形)。

MATRIX / matrix:以矩陣繪製,可用來動態縮放圖片。

最後提醒一個常被忽略的細節:drawable 資料夾裡的圖片檔名一律不能用大寫字母,否則會直接編譯失敗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved