Android的SwitchCompat和Switch

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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,422評論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,536評論 0 17
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,568評論 2 45
  • 1.block屬性的聲明 block屬性的聲明,需要copy修飾符,因?yàn)橹挥衏opy后的block才會在堆中,棧中...
    zhaihongxia閱讀 2,715評論 0 1
  • 人們沒了記憶就像死了一樣 黑叔走出了街道,慢慢的,從旁邊的城街道就自己走了出來,后面似乎有人跟著,這個(gè)人,腳步十分...
    不像話的故事閱讀 169評論 0 0