多个Switch缩略图

时间:2018-08-03 23:49:10

标签: android android-switch

在Android中,是否可能有一个Switch可以为拇指的选中和未选中状态设置不同的图像?

例如,当Switch处于未检查状态时,我希望拇指使用image_1.png,而当Switch处于检查状态时,我希望拇指使用image_2.png

enter image description here

我使用以下guideSwitch的拇指设置为图像,但是该图像适用于选中状态和未选中状态,我想为这两种图像分别创建一个图像状态。

谢谢!

1 个答案:

答案 0 :(得分:0)

this post中,您可以执行以下操作:

yourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // Set to image_2.png
            yourSwitch.setThumbResource(image2); // <-- Using an image from drawable
        } else {
            // Set to image_1.png
            yourSwitch.setThumbResource(image1);
        }
    }
});

并且如果您需要将Switch转换为程序形式:

Switch yourSwitch = (Switch) findViewById(R.id.yourSwitch);