Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 教學:用 RadioButton 實作圖文上下或左右排列

編輯:Android 開發實例

在 Android 開發中,經常會需要用到同時帶有圖片與文字的按鈕。本文就來示範如何利用 RadioButton,輕鬆做出圖片與文字「上下排列」或「左右排列」的效果。程式碼非常簡單,就不多作解釋了。

版面配置如下,主要用來展示 RadioButton 的用法:

 <?xml version="." encoding="utf-"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:button="@null" android:drawableTop="@drawable/ic_launcher" android:text="Test Button" /> </RelativeLayout>

成品效果如下圖:

這裡的關鍵屬性是 android:drawableTop,它會把圖片放在文字上方;如果想改成左右排列,只要換成 android:drawableLeft 或 android:drawableRight 即可。搭配 android:button="@null" 隱藏原本的圓形選擇鈕,就能得到乾淨的圖文按鈕。

以上程式碼沒有特別寫註解,相信大家都能看懂。若有任何問題,歡迎留言討論。

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