Androidselectorshape無效問題

雲驛站發表於2016-11-24

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qingfeng812/article/details/53320853

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="16dp" android:bottomRightRadius="16dp" android:topLeftRadius="16dp" android:topRightRadius="16dp"></corners>
            <stroke android:width="1dp" android:color="#999999"></stroke>
            <solid android:color="#fefefe"></solid>
        </shape>
    </item>

  
    <item android:state_selected="true">

        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="17dp" android:bottomRightRadius="17dp" android:topLeftRadius="17dp" android:topRightRadius="17dp"></corners>
            <stroke android:width="1dp" android:color="#999999"></stroke>
            <solid android:color="#cccccc"></solid>
        </shape>

    </item>

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="17dp" android:bottomRightRadius="17dp" android:topLeftRadius="17dp" android:topRightRadius="17dp"></corners>
            <stroke android:width="1dp" android:color="#999999"></stroke>
            <solid android:color="#cccccc"></solid>
        </shape>

    </item>


</selector>

//上面的selector是不能生效的!!!!


//正確的案例如下:【原因是順序問題】


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_selected="true">

        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="17dp" android:bottomRightRadius="17dp" android:topLeftRadius="17dp" android:topRightRadius="17dp"></corners>
            <stroke android:width="1dp" android:color="#999999"></stroke>
            <solid android:color="#cccccc"></solid>
        </shape>

    </item>

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="17dp" android:bottomRightRadius="17dp" android:topLeftRadius="17dp" android:topRightRadius="17dp"></corners>
            <stroke android:width="1dp" android:color="#999999"></stroke>
            <solid android:color="#cccccc"></solid>
        </shape>

    </item>
    //位置應該放在最底部,保證上面的效果不被覆蓋
    <item>
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="16dp" android:bottomRightRadius="16dp" android:topLeftRadius="16dp" android:topRightRadius="16dp"></corners>
            <stroke android:width="1dp" android:color="#999999"></stroke>
            <solid android:color="#fefefe"></solid>
        </shape>
    </item>
</selector>


相關文章