Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yjiang2009 opened this issue Oct 13, 2016 · 1 comment
Open

Comments

@yjiang2009
Copy link

yjiang2009 commented Oct 13, 2016

<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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant