Android的SwitchCompat和Switch

SwitchCompat

java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
↳ android.widget.CompoundButton
↳ android.support.v7.widget.SwitchCompat

属性相关方法作用
android:showTextsetShowText(boolean)Whether to draw on/off text.
android:splitTracksetSplitTrack(boolean)Whether to split the track and leave a gap for the thumb drawable.
android:switchMinWidthsetSwitchMinWidth(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:switchPaddingsetSwitchPadding(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:switchTextAppearancesetSwitchTextAppearance(Context,int)TextAppearance style for text displayed on the switch thumb.
android:textOffsetTextOff(CharSequence)Text to use when the switch is in the unchecked/”off” state.
android:textOnsetTextOn(CharSequence)Text to use when the switch is in the checked/”on” state.
android:textStylesetSwitchTypeface(Typeface)Style (bold, italic, bolditalic) for the text.
android:thumbsetThumbResource(int)Drawable to use as the “thumb” that switches back and forth.
android:thumbTextPaddingsetThumbTextPadding(int)Amount of padding on either side of text within the switch thumb.
android:thumbTintsetThumbTintList(ColorStateList)Tint to apply to the thumb.
android:thumbTintModesetThumbTintMode(PorterDuff.Mode)Blending mode used to apply the thumb tint.
android:tracksetTrackResource(int)Drawable to use as the “track” that the switch thumb slides within.
android:trackTintsetTrackTintList(ColorStateList)Tint to apply to the track.
android:trackTintModesetTrackTintMode(PorterDuff.Mode)Blending mode used to apply the track tint.
android:typefacesetSwitchTypeface(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

属性相关方法作用
android:showTextsetShowText(boolean)Whether to draw on/off text.
android:splitTracksetSplitTrack(boolean)Whether to split the track and leave a gap for the thumb drawable.
android:switchMinWidthsetSwitchMinWidth(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:switchPaddingsetSwitchPadding(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:switchTextAppearancesetSwitchTextAppearance(Context,int)TextAppearance style for text displayed on the switch thumb.
android:textOffsetTextOff(CharSequence)Text to use when the switch is in the unchecked/”off” state.
android:textOnsetTextOn(CharSequence)Text to use when the switch is in the checked/”on” state.
android:textStylesetSwitchTypeface(Typeface)Style (bold, italic, bolditalic) for the text.
android:thumbsetThumbResource(int)Drawable to use as the “thumb” that switches back and forth.
android:thumbTextPaddingsetThumbTextPadding(int)Amount of padding on either side of text within the switch thumb.
android:thumbTintsetThumbTintList(ColorStateList)Tint to apply to the thumb.
android:thumbTintModesetThumbTintMode(PorterDuff.Mode)Blending mode used to apply the thumb tint.
android:tracksetTrackResource(int)Drawable to use as the “track” that the switch thumb slides within.
android:trackTintsetTrackTintList(ColorStateList)Tint to apply to the track.
android:trackTintModesetTrackTintMode(PorterDuff.Mode)Blending mode used to apply the track tint.
android:typefacesetSwitchTypeface(Typeface)Typeface (normal, sans, serif, monospace) for the text.

在使用中发现Switch控件会出现按钮滑动一点点手指松开后,按钮停留在手指离开的位置,按钮既不处于true也不处于false状态,此时也无返回值,而SwitchCompat会判断此时滑动位置,将按钮滑动到底或者返回初始状态。 SwitchCompat和Switch大部分xml属性相同,但是在写xml属性是Switch的属性是以android开头,而SwitchCompat则是app开头的,两者switchTextAppearance的书写也有所区别。并且在开发中发现,由于SwitchCompat是v7包中的,则必须APP主题是以AppCompat下的,否则无选择效果,不可点击。

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="关"
                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="关"
                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>
    原文作者:阳翟后生
    原文地址: https://www.jianshu.com/p/6aea67eaf7a5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞