Switch
Switch結構說明
Step1 設計Switch樣式,並放入drawable資料夾裡
thumb樣式
switch on樣式(switch_thumb_activated.9.png)
switch off樣式(switch_thumb_off.9.png)
switch disable樣式(switch_thumb_disabled.9.png)
switch pressed樣式(switch_thumb_pressed.9.png)
track背景樣式
track default樣式(switch_bg.9.png)
track disable樣式(switch_bg_disabled.9.png)
track focused樣式(switch_bg_focused.9.png)
Step2 在drawable資料夾中新增switch_thumb.xml及switch_track.xml
switch_thumb.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_enabled="false" android:drawable="@drawable/switch_thumb_disabled" /> | |
<item android:state_pressed="true" android:drawable="@drawable/switch_thumb_pressed" /> | |
<item android:state_checked="true" android:drawable="@drawable/switch_thumb_activated" /> | |
<item android:drawable="@drawable/switch_thumb_off" /> | |
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled" /> | |
<item android:state_focused="true" android:drawable="@drawable/switch_bg_focused" /> | |
<item android:drawable="@drawable/switch_bg" /> | |
</selector> |
Step3 在activity_main.xml中增加switch元件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Switch | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textOff="OFF" | |
android:textOn="ON" | |
android:thumb="@drawable/switch_thumb" | |
android:track="@drawable/switch_track" | |
android:showText="true" | |
android:switchMinWidth="96dp" | |
android:thumbTextPadding="12dp" /> |
android:switchMinWidth才是更改Switch外觀的寬度
如果要更改Switch內的字體顏色:
1,在Style.xml裡新增字體的樣式,顏色、字體大小等
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style name="switch_text" parent="TextAppearance.AppCompat.Medium"> | |
<item name="android:textColor">#ffffff</item> | |
<item name="android:textSize">14sp</item> | |
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Switch | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textOff="OFF" | |
android:textOn="ON" | |
android:thumb="@drawable/switch_thumb" | |
android:track="@drawable/switch_track" | |
android:showText="true" | |
android:switchMinWidth="96dp" | |
android:thumbTextPadding="12dp" | |
android:switchTextAppearance="@style/switch_text" /> |
執行結果:
註:此範例為線上元件製作網站所產出的樣式(此網站目前無法使用---2017/11/21更新)
留言
張貼留言