SwitchCompat
java.lang.Object
? android.view.View
? android.widget.TextView
? android.widget.Button
? android.widget.CompoundButton
? android.support.v7.widget.SwitchCompat
屬性 | 相關(guān)方法 | 作用 |
---|---|---|
android:showText | setShowText(boolean) | Whether to draw on/off text. |
android:splitTrack | setSplitTrack(boolean) | Whether to split the track and leave a gap for the thumb drawable. |
android:switchMinWidth | setSwitchMinWidth(int) | Minimum width for the switch component。 Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". |
android:switchPadding | setSwitchPadding(int) | Minimum space between the switch and caption text。Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". |
android:switchTextAppearance | setSwitchTextAppearance(Context,int) | TextAppearance style for text displayed on the switch thumb. |
android:textOff | setTextOff(CharSequence) | Text to use when the switch is in the unchecked/"off" state. |
android:textOn | setTextOn(CharSequence) | Text to use when the switch is in the checked/"on" state. |
android:textStyle | setSwitchTypeface(Typeface) | Style (bold, italic, bolditalic) for the text. |
android:thumb | setThumbResource(int) | Drawable to use as the "thumb" that switches back and forth. |
android:thumbTextPadding | setThumbTextPadding(int) | Amount of padding on either side of text within the switch thumb. |
android:thumbTint | setThumbTintList(ColorStateList) | Tint to apply to the thumb. |
android:thumbTintMode | setThumbTintMode(PorterDuff.Mode) | Blending mode used to apply the thumb tint. |
android:track | setTrackResource(int) | Drawable to use as the "track" that the switch thumb slides within. |
android:trackTint | setTrackTintList(ColorStateList) | Tint to apply to the track. |
android:trackTintMode | setTrackTintMode(PorterDuff.Mode) | Blending mode used to apply the track tint. |
android:typeface | setSwitchTypeface(Typeface) | Typeface (normal, sans, serif, monospace) for the text. |
Switch
java.lang.Object
? android.view.View
? android.widget.TextView
? android.widget.Button
? android.widget.CompoundButton
? android.widget.Switch
屬性 | 相關(guān)方法 | 作用 |
---|---|---|
android:showText | setShowText(boolean) | Whether to draw on/off text. |
android:splitTrack | setSplitTrack(boolean) | Whether to split the track and leave a gap for the thumb drawable. |
android:switchMinWidth | setSwitchMinWidth(int) | Minimum width for the switch component. Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". |
android:switchPadding | setSwitchPadding(int) | Minimum space between the switch and caption text。Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". |
android:switchTextAppearance | setSwitchTextAppearance(Context,int) | TextAppearance style for text displayed on the switch thumb. |
android:textOff | setTextOff(CharSequence) | Text to use when the switch is in the unchecked/"off" state. |
android:textOn | setTextOn(CharSequence) | Text to use when the switch is in the checked/"on" state. |
android:textStyle | setSwitchTypeface(Typeface) | Style (bold, italic, bolditalic) for the text. |
android:thumb | setThumbResource(int) | Drawable to use as the "thumb" that switches back and forth. |
android:thumbTextPadding | setThumbTextPadding(int) | Amount of padding on either side of text within the switch thumb. |
android:thumbTint | setThumbTintList(ColorStateList) | Tint to apply to the thumb. |
android:thumbTintMode | setThumbTintMode(PorterDuff.Mode) | Blending mode used to apply the thumb tint. |
android:track | setTrackResource(int) | Drawable to use as the "track" that the switch thumb slides within. |
android:trackTint | setTrackTintList(ColorStateList) | Tint to apply to the track. |
android:trackTintMode | setTrackTintMode(PorterDuff.Mode) | Blending mode used to apply the track tint. |
android:typeface | setSwitchTypeface(Typeface) | Typeface (normal, sans, serif, monospace) for the text. |
在使用中發(fā)現(xiàn)Switch控件會出現(xiàn)按鈕滑動一點(diǎn)點(diǎn)手指松開后,按鈕停留在手指離開的位置,按鈕既不處于true也不處于false狀態(tài),此時(shí)也無返回值,而SwitchCompat會判斷此時(shí)滑動位置,將按鈕滑動到底或者返回初始狀態(tài)。 SwitchCompat和Switch大部分xml屬性相同,但是在寫xml屬性是Switch的屬性是以android開頭,而SwitchCompat則是app開頭的,兩者switchTextAppearance的書寫也有所區(qū)別。并且在開發(fā)中發(fā)現(xiàn),由于SwitchCompat是v7包中的,則必須APP主題是以AppCompat下的,否則無選擇效果,不可點(diǎn)擊。
Switch
<Switch
android:id="@+id/switch1"
android:layout_width="368px"
android:layout_height="100px"
android:showText="true"
android:switchTextAppearance="@style/SwitchTxt"
android:textOff="開"
android:textOn="關(guān)"
android:thumb="@drawable/thumb"
android:track="@android:color/transparent" />
SwitchCompat
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchcompat"
android:layout_width="368px"
android:layout_height="100px"
android:textOff="開"
android:textOn="關(guān)"
android:checked="true"
android:thumb="@drawable/thumb"
app:showText="true"
app:switchTextAppearance="@style/switch.txt"
app:theme="@style/MySwitch"
app:track="@android:color/transparent" />
style
<style name="SwitchTxt" parent="@android:style/TextAppearance.Material.Widget">
<item name="android:textSize">36px</item>
<item name="android:textColor">@android:color/white</item>
</style>
<style name="MySwitch" parent="Theme.AppCompat.Light">
<item name="colorControlActivated">@android:color/transparent</item>
<item name="colorSwitchThumbNormal">@android:color/transparent</item>
<item name="android:colorForeground">@android:color/transparent</item>
</style>
<style name="switch.txt" parent="Theme.AppCompat.Light">
<item name="android:textSize">36px</item>
<item name="android:textColor">@android:color/white</item>
</style>