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

Android 新手筆記:ImageButton 與 Button 的版面配置技巧

編輯:關於 Android 程式設計

剛開始接觸 Android 版面配置時,最常遇到的就是「怎麼把按鈕放到我想要的位置」。本文整理兩個實用的小技巧,幫助你靈活擺放圖片按鈕與一般按鈕。

加入圖片按鈕(ImageButton)

使用 ImageButton 時,透過 android:layout_marginLeft 與 android:layout_marginTop 就能精準控制它在你指定範圍內的位置:

<ImageButton
        android:id="@+id/set"
        android:layout_width="190dp"
        android:layout_height="67dp"
        android:layout_marginLeft="45dip"
        android:layout_marginTop="195dip"
        android:scaleType="fitXY"
        android:src="@drawable/ing" />

加入一般按鈕(Button)

一般 Button 的寫法類似,也能用邊距屬性定位:

<Button
        android:id="@+id/start"
        android:layout_width="190dip"
        android:layout_height="45dip"
        android:layout_marginLeft="50dip"
        android:layout_marginTop="125dip"
        android:background="#00000000" />

關鍵技巧:把按鈕放進 RelativeLayout

上面兩個按鈕都放在 <RelativeLayout> 裡,如此一來就能透過邊距把它們擺到你要的位置。而若你希望按鈕完全透明、只看得到圖片內容,只要加上這一句即可:

android:background="@null"

這兩個小設定看似簡單,卻是打造自訂 UI 時經常派上用場的基礎功。

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