Switch自定义滑道和指示头

在这里插入图片描述

thumbTint为指示头颜色

 <Switch
                        android:id="@+id/send_switch"
                        android:layout_marginLeft="10dp"
                        android:trackTint="@color/text_hint"
                        android:thumbTint="@color/white"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center" />

代码控制滑道颜色

 sendSwitch = (Switch) findViewById(R.id.send_switch);
        sendSwitch.setTrackResource(R.mipmap.switch_gray);
        sendSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
                if (isChecked) {
    
    
                    urgency = 1;
                    sendSwitch.setTrackResource(R.mipmap.switch_blue);
                } else {
    
    
                    urgency = 0;
                    sendSwitch.setTrackResource(R.mipmap.switch_gray);
                }
            }
        });

猜你喜欢

转载自blog.csdn.net/xiyangyang8110/article/details/124150335