自定義switch首先要熟悉兩個屬性,thumb與trace
-. thumb 代表按鈕的意思,就是switch左右兩邊那個
-. trace 代表軌跡的意思,就是thumb在來回滑動過程中變化的軌跡
. 自定義thumb
自定義trace關閉狀態
xmlns:android="">
android:color="#dbdbdb"/>
android:radius="20dp"/>
shape>
自定義trace開啟狀態
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="">
android:color="#fe8a01"/>
android:radius="20dp"/>
shape>
自定義trace選擇器
xmlns:android="">
android:drawable="@drawable/shape_switch_track_open"
android:state_checked="true"/>
android:drawable="@drawable/shape_switch_track_close"
android:state_checked="false"/>
android:drawable="@drawable/shape_switch_track_close"
/>
selector>
最終的應用
"@+id/st_switch"
android:layout_width="wrap_content"
android:layout_margintop="10dp"
android:layout_marginright="10dp"
android:thumb="@drawable/shape_switch_thumb"
android:track="@drawable/shape_switch_track_selector"
android:layout_height="wrap_content" />
switch的大小是跟thumb的大小有關
![](https://pic.w3help.cc/4a5/18802977f0d257a63ad3e58cd0e06.jpeg)
Android自定義控制項 自定義屬性
自定義屬性的過程 1.在res values資料夾中建立attrs的xml檔案。2.寫入標籤,定義子標籤attr,放入自定義屬性的名稱。format 可以用 來同時使用 1 reference 參考某一資源id 2 color 顏色值 3 boolean 布林值 4 dimension 尺寸值 帶有...
Android自定義View 自定義元件
自繪控制項也分兩種,自定義元件和自定義容器,自定義元件是繼承view類,自定義容器時繼承viewgrounp 今天主要分析下自定義元件 還是舉個例子來的實際些,假如我們要畫乙個最簡單的textview,首先想到的就是canvas.drawtext 方法,怎麼畫了?還是得一步一步來 1 寫乙個myte...
Android自定義控制項之自定義View 二
效果如下圖 1 自定義ringview繼承view新增其構造方法並建立畫筆 public class ringview extends view protected boolean isrunning false public ringview context context public ring...