編輯:關於 Android 程式設計
接到的需求很簡單:一條橫線上等距排布 n 個節點,每個節點上方一個圓、下方一段文字;選中哪一個,圓變成藍色空心圈、文字也變藍。這種控制項在系統裡找不到現成的,正好拿來做自定義 View 的練習。

先把畫面上有哪些東西列出來:一條基準線、n 個圓(未選中是實心小圓,選中是較粗的空心圓)、n 段文字。接著把需要的成員變數定下來:
· 資料:String[] tabNames 提供選項文字,int selectedIndex 記錄當前選中序號
· 樣式:mTextSize 字級、mColorTextDef 未選中文字色、mColorDef 線與圓的顏色、mColorSelected 選中色
· 尺寸:mLineHight 線粗、mCircleHight 圓直徑、mCircleSelStroke 選中空心圓筆畫寬度、mMarginTop 圓與文字的間距
兩個關鍵的計算量:lineLength 等於控件寬度減去左右各半個圓半徑,這樣線的兩端才會正好落在兩側圓心上;splitLength = lineLength / (n - 1) 則是相鄰圓之間的間距。
這裡有個容易忽略的細節:線長必須等測量完成、寬度確定之後才能算,所以這些計算要放在 onMeasure() 裡,不能寫在構造方法中。
於是流程是:繼承 View → 覆寫帶 AttributeSet 的構造方法讀取自定義屬性 → 在 onDraw() 中用 drawLine、drawCircle、drawText 把輪廓畫出來。
佈局先簡單放進去:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dip">
<com.openxu.st.slidetab.SlideTab
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aaff0000" />
</LinearLayout>
刻意給了半透明紅底,方便確認控件實際佔了多大範圍。

此時會發現:明明寫的是 wrap_content,紅色區塊卻填滿整個螢幕高度。這正是自定義 View 一定要處理的測量問題。
這個控件的高度應該是「圓的直徑 + 文字高度 + 兩者間距」,寬度則直接沿用父容器給的結果。把自定義屬性透過 declare-styleable 開放出來:
<declare-styleable name="SlideTab">
<attr name="textcolordef" format="color" />
<attr name="textsize" format="dimension" />
<attr name="defcolor" format="color" />
<attr name="selectedcolor" format="color" />
<attr name="linthight" format="dimension" />
<attr name="circlehight" format="dimension" />
<attr name="circleselstroke" format="dimension" />
<attr name="mmargintop" format="dimension" />
<attr name="tabnames" format="reference" />
</declare-styleable>
佈局中的用法:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:openxu="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dip">
<com.openxu.st.slidetab.SlideTab
android:id="@+id/slideTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textsize="15sp"
openxu:circlehight="20dip"
openxu:circleselstroke="5dip"
openxu:defcolor="#EAEAEA"
openxu:linthight="2dip"
openxu:mmargintop="15dip"
openxu:selectedcolor="#5CBB8C"
openxu:tabnames="@array/tab_names"
openxu:textcolordef="#A4A4A4" />
</LinearLayout>
文字清單放在 res/values/array.xml 的 string-array 裡,程式從 TypedArray.getTextArray() 取出後轉成字串陣列即可。

高度正常之後,剩下的就是滑動手勢與位置換算:攔截 onTouchEvent 的 ACTION_DOWN / MOVE,把觸發點的 x 座標除以 splitLength 並四捨五入,就得到應該選中的索引;索引若有變化,更新 selectedIndex 並 invalidate() 重繪。若想讓藍色圓跟手 smoothly 移動,可以在 MOVE 階段記錄浮動偏移量,在 onDraw 時把選中圓畫在手指正上方,抬起時再吸附到最近的節點。
這個例子想傳達的重點其實只有三件事:自定義屬性要透過 styleable 開放出來、wrap_content 必須自己在 onMeasure 裡算、所有依賴寬度的幾何計算都要延後到測量之後。把這三點掌握好,大部分類似的選擇器、進度條、刻度尺都能照樣寫出來。
https://github.com/openXu/SlidingTab
手機QQ怎麼安裝?三種安裝方式與安裝目錄位置查詢
手機QQ的安裝方式已經比早期簡單很多,但仍有使用者卡在「裝在哪裡」這個問題上——尤其是需要備份資料庫或清理殘留檔案的時候。以下整理三種安裝路徑,以及安裝位置的查找方式。手
Android ListView 四種 Adapter 用法對比與選型指南
ListView 是 Android 開發中使用頻率極高的 UI 控件。它的每一個子項既可以是一行簡單的字符串,也可以是一組 View 的複合布局,展示形式完全由開發者定
Android Intent 機制與常見用法:頁面跳轉、傳值與系統功能呼叫
Activity 與 Intent Activity 是 Android 應用程式的核心元件,負責畫面與使用者互動。Activity 之間的跳轉與資料傳遞,通常透過 In
Android AnimationListener 監聽動畫狀態的用法
AnimationListener 的作用 AnimationListener 用來監聽動畫的生命週期,包括開始、結束與重複。透過它,可以在動畫完成後執行額外處理,例如移