kyleduo / SwitchButton

A cute widget of Switch Button for you to create beautiful and friendly UI.
4.7k stars 863 forks source link

2.3手机上用backdrawble,切换时背景无法改变,求助! #76

Open yjiang2009 opened 7 years ago

yjiang2009 commented 7 years ago
<com.ftd2009.switchbuttoniosdemo.SwitchButton
    android:id="@+id/sb_ios"
    style="@style/SwitchButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:kswAnimationDuration="300"
    app:kswBackDrawable="@drawable/ios_back_drawable"
    app:kswBackMeasureRatio="2"
    app:kswThumbDrawable="@drawable/ios_thumb_selector"/>

用的是app:kswBackDrawable,@drawable/ios_back_drawable是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:state_checked="true">
        <shape>
            <solid android:color="#3097fd"/>
            <corners android:radius="15dp"/>
        </shape>
    </item>
    <item android:state_enabled="false" android:state_checked="false">
        <shape>
            <solid android:color="#ff0000"/>
            <corners android:radius="25dp"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape>
            <solid android:color="#000000"/>
            <corners android:radius="35dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#ffff00"/>
            <corners android:radius="45dp"/>
        </shape>
    </item>
</selector>

但是在2.3上切换时改变不了背景色,4.0上都是好的,是什么问题啊。我用的nineold库可以在2.3上有动画,但是切换时背景色改变不了,HELP! 看了看源码,应该是下面这块代码有问题:

if (!mIsBackUseDrawable && mBackColor != null) {
            mCurrBackColor = mBackColor.getColorForState(getDrawableState(), mCurrBackColor);
            mNextBackColor = mBackColor.getColorForState(nextState, mCurrBackColor);
        } else {
            if (mBackDrawable instanceof StateListDrawable && mFadeBack) {
                mBackDrawable.setState(nextState);
                mNextBackDrawable = mBackDrawable.getCurrent().mutate();
            } else {
                mNextBackDrawable = null;
            }
            setDrawableState(mBackDrawable);
            if (mBackDrawable != null) {
                mCurrentBackDrawable = mBackDrawable.getCurrent().mutate();
            }
        } 
yjiang2009 commented 7 years ago

由于drawable在2.3上异常,于是尝试改成用kswBackColor。在2.3上正常了。但还是不知道为什么用drawble会失效。