美文网首页
仿微信Switch样式

仿微信Switch样式

作者: 编程的猫 | 来源:发表于2020-03-20 16:11 被阅读0次

    先上效果:


    image.png
    image.png

    直接上代码:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/switch_ios_track_on" android:state_checked="true" />
        <item android:drawable="@drawable/switch_ios_track_off" android:state_checked="false" />
    </selector>
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#7ED53D" />
        <corners android:radius="20dp" />
    </shape>
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@android:color/darker_gray" />
        <corners android:radius="20dp" />
    </shape>
    

    在activity中使用:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".wheel.RvActivity">
    
      <Switch
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:checked="false"
          android:padding="8dp"
          android:text="开启WIFI"
          android:textColor="@android:color/black"
          android:layout_marginTop="80dp"
          android:layout_centerInParent="true"
          android:id="@+id/sw_auto_button"
          android:thumb="@drawable/switch_ios_thumb"
          android:track="@drawable/switch_ios_track_selector"/>
    
    </RelativeLayout>
    
     Switch swAutoPlay = findViewById(R.id.sw_auto_button);
    
            swAutoPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //SPUtils.getInstance().put(Constants.AUTO_PALY_IN_WIFI, isChecked);
                    if (isChecked) {
                        Log.e(TAG, "onCheckedChanged: 开启" + isChecked);
                    } else {
                        Log.e(TAG, "onCheckedChanged: 关闭" + isChecked);
                    }
                }
            });
    
    

    相关文章

      网友评论

          本文标题:仿微信Switch样式

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