Android的SwitchCompat和Switch

作者: 阳翟后生 | 来源:发表于2017-09-07 12:20 被阅读2354次

    SwitchCompat

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

    属性 相关方法 作用
    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

    属性 相关方法 作用
    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控件会出现按钮滑动一点点手指松开后,按钮停留在手指离开的位置,按钮既不处于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>
    

    相关文章

      网友评论

        本文标题:Android的SwitchCompat和Switch

        本文链接:https://www.haomeiwen.com/subject/osdpjxtx.html