編輯:關於 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():依據子按鈕的位置回傳對應的圆角半徑陣列。
第五步:效果預覽

Android ImageView 圖片旋轉與縮放實作
ImageView 圖片處理 ImageView 主要用來顯示圖片,也可以搭配 Matrix 做旋轉、縮放等效果。scaleType 屬性則可控制圖片在控制項中的顯示方式
Android TextView 進階顯示技巧合集
自定義字體Android 可透過 Typeface 載入 TTF 字體。將字體檔放入 assets 的 fonts 目錄,再以 createFromAsset 建立 Ty
Android 繪圖實作:用三次樣條畫出平滑曲線並加上漸變填色
做過圖表的人大概都有過這個經驗:資料點明明都對,畫出來的線卻像鋸齒一樣一折一折。這篇要處理的就是這個問題——如何把一堆離散數據點,畫成一條視覺上順滑的曲線,並且在下方的封
Android 系統架構詳解:從應用框架到 Linux 內核
本文談的是應用層之下的整個系統內部結構。與常見的「四層架構」說法(應用層、Framework、執行庫與執行環境、Linux 內核)不同,這裡把系統內部的呼叫鏈路切得更細。