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

細談 Android 中的各種對齊方式

編輯:關於 Android 程式設計

在看 API 時經常會遇到 Start 對齊、End 對齊。Start 對齊主要是為了在不同 textDirection(文字排列方向)下更好地對齊:當 textDirection 為 LTR(從左到右)時,Start 對齊等同左對齊;當為 RTL(從右到左)時,Start 對齊則等同右對齊,End 同理。

文字對齊:gravity

使用 gravity 可實現 View 內文字的對齊方式。官方解釋如下:

android:gravity:Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.

意思是:當文字比 View 小時,指定如何依 View 的 x、y 軸對齊。屬性值可以是下列一個或多個,多個時以 '|' 隔開:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal、start、end。其中既有上下左右這種基本型,也有像 left|bottom 這樣的組合型。

左對齊:

<TextView
android:layout_width="100dip"
android:layout_height="60dip"
android:layout_marginTop="10dip"
android:background="#ffff33"
android:gravity="left"
android:text="left" />

左下角對齊:

<TextView
android:layout_width="100dip"
android:layout_height="60dip"
android:layout_marginTop="10dip"
android:background="#ffff33"
android:gravity="left|bottom"
android:text="leftbottom" />

LinearLayout 對齊:gravity

官方解釋:

gravity:Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

意思是:規定一個對象如何依 x、y 軸在自己的範圍內擺放內容。可用值同上,多個以 '|' 隔開。

居右對齊:

<LinearLayout
android:layout_width="match_parent"
android:background="#0000ff"
android:layout_height="0dip"
android:gravity="right"
android:layout_weight="1" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="right" />
</LinearLayout>

居左下角對齊:

<LinearLayout
android:layout_width="match_parent"
android:background="#ffff33"
android:layout_height="0dip"
android:layout_marginTop="10dip"
android:gravity="left|bottom"
android:layout_weight="1" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="left_bottom" />
</LinearLayout>

RelativeLayout 相對位置對齊

RelativeLayout 主要用於相對位置對齊,常用屬性如下:

layout_above 將該元件底部設到指定 ID 對象上方;layout_below 設到下方;layout_alignBaseline 與指定 ID 對象基線對齊;layout_alignBottom 底部與指定 ID 對象底部對齊;layout_alignLeft、layout_alignRight、layout_alignTop 分別與指定 ID 對象的左、右、頂部對齊;layout_alignStart 開始(左)與指定對象開始位置對齊;layout_alignEnd 結束(右)與指定對象結束位置對齊;layout_toLeftOf 將該元件右邊放到指定 ID 對象左邊;layout_toStartOf 類似;layout_toRightOf 將該元件左邊放到指定 ID 對象右邊;layout_centerHorizontal 水平居中;layout_centerInParent 置於父對象中央;layout_centerVertical 垂直居中;layout_alignParentLeft 左邊與父對象左邊對齊;layout_alignParentRight 右邊與父對象右邊對齊;layout_alignParentStart、layout_alignParentTop、layout_alignParentEnd、layout_alignParentBottom 則分別與父對象對應邊對齊。

例子一:把一個 button 放到另一個 button 右邊。

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnStand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標准物1" />
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnStand"
android:text="right->標准物1" />
</RelativeLayout>

例子二:把一個 button 放到另一個 button 的右下角。

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnStand2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標准物2" />
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/btnStand2"
android:layout_toRightOf="@id/btnStand2"
android:text="right_bottom->標准物2" />
</RelativeLayout>

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