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

Android 進階:仿新浪微博風格自訂 RadioButton 樣式

編輯:Android 開發實例

Android 內建的 RadioButton 長什麼樣,用過的人都懂——那顆圓點配邊框的造型,放進任何有設計感的介面裡都顯得格格不入。新浪微博當年那組搜索用的分段式切換鈕就漂亮多了,這篇就照著那個思路做一遍。

先決條件:把預設的圓點藏起來

自訂樣式的關鍵有兩點:一是把 android:button 設成 @null,讓系統不要畫那顆預設圓點;二是用 android:background 換上自己的 selector 或九宮格圖。左右兩顆按鈕共用同一組前置設定,只差背景圖不同——左邊那顆用左半圓角,右邊那顆用右半圓角,拼起來就是一個完整的膠囊。

版面配置

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

    <RadioGroup
        android:id="@+id/searchRadioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:orientation="horizontal"
        >

        <RadioButton
            android:id="@+id/searchRadioShare"
            android:tag="radio_button0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="17.0sp"
            android:text="搜索微博"
            style="@style/CustomLeftRadioBtn" />

        <RadioButton
            android:id="@+id/searchRadioUser"
            android:tag="radio_button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="17.0sp"
            android:text="搜索用戶"
            style="@style/CustomRightRadioBtn" />

    </RadioGroup>

</LinearLayout>

兩個 RadioButton 各自掛上不同的 style,並用 layout_weight="1" 讓它們等分寬度。順帶一提,android:tag 在這裡是個好用的技巧:後續要判斷是哪一顆被按,讀 tag 往往比比對 id 更直觀。

定義 style

在 res/values/styles.xml 裡分別描述左右兩顆按鈕:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="CustomLeftRadioBtn">
        <item name="android:button">@null</item>
        <item name="android:drawableLeft">@drawable/search_radio_1</item>
        <item name="android:background">@drawable/search_left</item>
        <item name="android:gravity">center_vertical</item>
    </style>

    <style name="CustomRightRadioBtn">
        <item name="android:button">@null</item>
        <item name="android:drawableLeft">@drawable/search_radio_1</item>
        <item name="android:background">@drawable/search_right</item>
        <item name="android:gravity">center_vertical</item>
    </style>

</resources>

兩個樣式唯一的差異就是 background 指向左半還是右半。若想連文字與圖示的雙色切換一起做,可以把 search_left/search_right 改成 selector,用 android:state_checked 分別對應選中與未選中的圖:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_checked="true"  android:drawable="@drawable/search_left_selected" />
    <item                              android:drawable="@drawable/search_left_normal" />
</selector>

掛上點擊事件

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class RadioButtonDemo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button searchRadioShare = (Button) findViewById(R.id.searchRadioShare);
        Button searchRadioUser  = (Button) findViewById(R.id.searchRadioUser);

        searchRadioShare.setOnClickListener(l);
        searchRadioUser.setOnClickListener(l);
    }

    private View.OnClickListener l = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.searchRadioShare) {
                Toast.makeText(RadioButtonDemo.this, "搜索微博", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(RadioButtonDemo.this, "搜索用戶", Toast.LENGTH_LONG).show();
            }
        }
    };
}

兩個容易忽略的細節

  • 左右兩張背景圖要用同一個高度、同一組圓角半徑,否則接縫處會出現高低差或白線。
  • 單純掛 OnClickListener 只能處理「點擊」;如果你需要同時監聽「被選中」的狀態變化(例如用方向鍵切換焦點時也要跟著換圖),改用 RadioGroup.OnCheckedChangeListener 會更合適。

改到這裡,那顆醜到不行的預設圓點就正式退場了。整套做法換到 ToggleButton 或自訂的 LinearLayout 上同樣適用,差別只在要不要自己維護 checked 狀態。

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