Customize SwitchButton image resources
masterA SwitchButton is composed of four distinct image layers. You can customize these either via XML attributes or programmatically.
The Four Components
- frameDrawable: The frame image that determines the button size and display area.
- stateDrawable: The image representing the ON or OFF state.
- stateMaskDrawable: A mask layer used to clip the
stateDrawableto a specific shape (e.g., for rounded corners). - sliderDrawable: The image of the sliding handle.
Method 1: XML Attributes
Use the app: namespace to define resources directly in your layout file:
<?xml version="1.0" encoding="utf-8"?>
<me.panpf.switchbutton.SwitchButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受推送"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>Method 2: Programmatic Setup
Use setDrawables(Drawable, Drawable, Drawable, Drawable) or setDrawableResIds(int, int, int, int) in your Java/Kotlin code to update the images dynamically.