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

透徹理解 android:gravity 屬性:取值、常數與效果圖解

編輯:關於 Android 程式設計

android:gravity 是控制元件內容對齊方式的重要屬性。本文整理了官方文件中的相關常量,並搭配兩個可直接套用的 XML 佈局示例,幫助你直觀理解每個取值的實際效果。

一、官方常量說明

以下是 Gravity 類別中幾個常見常量的含義(皆為 API Level 1 起提供,部分裁剪標記為 API 3 或 14):

  • TOP / BOTTOM:把物件推向容器的頂端/底端,不改變其大小。
  • LEFT / RIGHT:把物件推向容器的左側/右側,不改變其大小。
  • CENTER:在垂直與水平兩個軸上都置中。
  • CENTER_HORIZONTAL / CENTER_VERTICAL:僅在水平/垂直方向置中。
  • FILL:必要時撐開物件的水平與垂直尺寸,使其完全填滿容器。
  • FILL_HORIZONTAL / FILL_VERTICAL:僅在水平/垂直方向填滿容器。
  • CLIP_HORIZONTAL / CLIP_VERTICAL:將物件邊緣沿水平/垂直方向裁切至容器範圍(API 3)。
  • START / END:將物件推向容器的起點/終點(相對於版面方向,API 14)。
  • NO_GRAVITY:表示未設定任何 gravity。

此外還有 AXIS_CLIP、AXIS_PULL_BEFORE、AXIS_PULL_AFTER、RELATIVE_LAYOUT_DIRECTION 等底層控制位,供進階排版使用。

二、效果圖 1:單一屬性對比

下面的佈局把 top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal 等常見取值逐一放進固定高度的 TextView 中,方便對照差異:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView android:id="@+id/TextView01"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="top"
        android:gravity="top"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView02"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="bottom"
        android:gravity="bottom"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView03"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="left"
        android:gravity="left"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView04"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="right"
        android:gravity="right"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView05"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="center_vertical"
        android:gravity="center_vertical"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView06"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="fill_vertical"
        android:gravity="fill_vertical"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView07"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="center_horizontal"
        android:gravity="center_horizontal"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView08"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="fill_horizontal"
        android:gravity="fill_horizontal"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView09"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="center"
        android:gravity="center"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView10"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="fill"
        android:gravity="fill"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView11"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="clip_vertical"
        android:gravity="clip_vertical"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>

    <TextView android:id="@+id/TextView12"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:text="clip_horizontal"
        android:gravity="clip_horizontal"
        android:textColor="#ffffff"
        android:background="#ff0000"
        android:layout_margin="1px"/>
</LinearLayout>

三、效果圖 2:調整高度與邊距後的對比

把每個 TextView 的高度改為 50dp、邊距改為 2px、背景改為綠色,可以更明顯看出 top、bottom、center、fill 等取值在較高容器中的對齊差異。其結構與圖 1 完全相同,只是把 android:layout_height="50dp"、android:layout_margin="2px"、android:background="#00ff00" 套用至每個 TextView 即可。

本文作者:AMinfo。實際開發時,gravity 常用來控制文字在按鈕、 TextView 內部的對齊;搭配不同取值與容器尺寸,就能呈現多樣的排版效果。

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