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

動手玩轉Android AbsoluteLayout 絕對布局的座標定位用法

編輯:關於 Android 程式設計

在Android的諸多布局容器中,AbsoluteLayout 是相當有個性的一位。它採用的定位邏輯非常直白:直接依據每個子元件左上角的絕對座標,來決定元件在畫面上的落點。本文就帶大家一起把這個布局的用法摸清楚,供大家參考動手實作。

什麼是 AbsoluteLayout

AbsoluteLayout 又被稱為絕對布局管理器。它的核心思路就是透過 android:layout_x 與 android:layout_y 兩個屬性,為每個子元件標定精確的橫向與縱向座標。寫法簡單明瞭,一眼就能看懂元件擺在哪裡。

下面這段 XML 示範了如何在布局中放入四個按鈕,並讓它們沿著對角線依序錯開:

 <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" android:layout_x="0px" android:layout_y="0px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" android:layout_x="20px" android:layout_y="20px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" android:layout_x="40px" android:layout_y="40px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕4" android:layout_x="60px" android:layout_y="60px" /> </AbsoluteLayout> 

執行後的效果如下圖所示,四顆按鈕整齊地沿著斜向排列:

以上就是 AbsoluteLayout 的基本用法示範,希望對正在學習 Android 介面開發的朋友有所幫助。

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