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

Android 打造 iOS 風格的分段選擇控件 UISegmentedControl

編輯:關於 Android 程式設計

在 iOS 7 中出現過一種扁平風格的控件——分段選擇控件 UISegmentedControl。它由幾個被細線分隔的按鈕橫放或豎排而成,每次點擊都能切換不同的按鈕與對應的介面。比如 QQ 客戶端 V6.5.3 中,把消息頁與電話頁分離開來,用的就是這種設計思路。

可惜 Android 系統並沒有內建這類控件。不過我們可以透過自訂 RadioGroup 來模擬同樣的效果。所幸 GitHub 上已有現成的開源方案:
https://github.com/Kaopiz/android-segmented-control

實現步驟如下:

第一步:在 Android Studio 中導入相依

dependencies {
    compile 'info.hoang8f:android-segmented:1.0.6'
}

第二步:在佈局中引用控件

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:segmentedgroup="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--自訂的 RadioButton 單選按鈕組-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimenActionBarHeight"
        android:background="@color/colorActionbar"
        android:gravity="center"
        android:minHeight="40dp">

        <info.hoang8f.android.segmented.SegmentedGroup
            android:id="@+id/mSegmentedGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:layout_marginRight="14dp"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            segmentedgroup:sc_border_width="1dp"
            segmentedgroup:sc_checked_text_color="@color/colorActionbar"
            segmentedgroup:sc_corner_radius="10dp"
            segmentedgroup:sc_tint_color="@color/colorWhite">

            <RadioButton
                android:id="@+id/radioButtonOne"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="案例展示" />

            <RadioButton
                android:id="@+id/radioButtonTwo"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="導師風采" />
        </info.hoang8f.android.segmented.SegmentedGroup>
    </RelativeLayout>

    <!--內容區-->
    <FrameLayout
        android:id="@+id/foundFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

第三步:在程式碼中綁定 Fragment

public class FoundActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private SegmentedGroup mSegmentedGroup;
    private RadioButton radioButtonOne, radioButtonTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_found);
        initWidget();  // 初始化組件
        initData();    // 初始化資料
        initEvent();   // 初始化事件
    }

    private void initEvent() {
        radioButtonOne.setChecked(true);  // 默認選擇第一個(案例展示碎片)
    }

    private void initData() {
    }

    private void initWidget() {
        mSegmentedGroup = (SegmentedGroup) findViewById(R.id.mSegmentedGroup);  // 控件組
        radioButtonOne = (RadioButton) findViewById(R.id.radioButtonOne);      // 單選按鈕一
        radioButtonTwo = (RadioButton) findViewById(R.id.radioButtonTwo);      // 單選按鈕二
        mSegmentedGroup.setTintColor(Color.WHITE);          // 設置默認線條顏色及背景色
        mSegmentedGroup.setOnCheckedChangeListener(this);   // 綁定單選按鈕選擇監聽
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radioButtonOne:
                FragmentManager fm = this.getSupportFragmentManager();
                FragmentTransaction tr = fm.beginTransaction();
                tr.replace(R.id.foundFrameLayout, new CaseShowFragment());  // 案例展示碎片
                tr.commit();
                break;
            case R.id.radioButtonTwo:
                FragmentManager fm2 = this.getSupportFragmentManager();
                FragmentTransaction tr2 = fm2.beginTransaction();
                tr2.replace(R.id.foundFrameLayout, new TutorStyleFragment());  // 導師咨詢碎片
                tr2.commit();
                break;
        }
    }
}

寫到這裡,我們想要的效果基本就達成了。休息之餘,也別忘了翻翻源碼,看看它是如何運作的:

第四步:自訂 RadioGroup 源碼分析

SegmentedGroup 繼承自 RadioGroup,其核心在於透過 updateBackground() 為每個子按鈕動態切換背景。它使用 LayoutSelector 內部類,根據按鈕所在的方位(首個、中間、末個)套用不同的圓角配置,從而使整組按鈕呈現連成一體的圓角外觀。同時透過 ColorStateList 與 StateListDrawable,實現選中與未選中狀態之間的樣式切換。關鍵方法包括:

- initAttrs():讀取 sc_border_width、sc_corner_radius、sc_tint_color 等自訂屬性。
- updateBackground():遍歷所有子 View,調整邊距並套用狀態背景。
- setTintColor():動態改變主題色。
- LayoutSelector.getChildRadii():依據子按鈕的位置回傳對應的圆角半徑陣列。

第五步:效果預覽

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