自定义切换按钮的可绘制对象未居中

Drawable of a custom toggle button not centered

我正在尝试模拟一个带有切换按钮的复选框,因为我需要自定义整个小部件,所以我正在尝试下一个代码,但是图像位于左侧,我需要将其放置居中。

<ToggleButton
                    android:id="@+id/tb_legal_conditions"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:background="@drawable/checkbox_bg"
                    android:button="@drawable/checkbox"
                    android:checked="true"
                    android:gravity="center"
                    android:minHeight="0dp"
                    android:minWidth="0dp"
                    android:textOff="@null"
                    android:textOn="@null" />

checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="false" android:drawable="@color/transparent" />
    <item a<ndroid:state_checked="true" android:drawable="@drawable/ic_checked" />
    <item android:drawable="@color/transparent" />
</selector>

checkbox_bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/checkbox_bg"/>
    <corners
        android:radius="@dimen/checkbox_corner"
        android:bottomLeftRadius="@dimen/checkbox_corner"
        android:bottomRightRadius="@dimen/checkbox_corner"
        android:topLeftRadius="@dimen/checkbox_corner"
        android:topRightRadius="@dimen/checkbox_corner" />
    <size android:height="32dp" android:width="32dp"/>
</shape>

结果:

ic_checked是一个向量:

<vector android:height="24dp" android:viewportHeight="26.0"
    android:viewportWidth="26.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#005bab" android:pathData="m0.3,14c-0.2,-0.2 -0.3,-0.5 -0.3,-0.7s0.1,-0.5 0.3,-0.7l1.4,-1.4c0.4,-0.4 1,-0.4 1.4,0l0.1,0.1 5.5,5.9c0.2,0.2 0.5,0.2 0.7,0l13.4,-13.9h0.1v-0c0.4,-0.4 1,-0.4 1.4,0l1.4,1.4c0.4,0.4 0.4,1 0,1.4l0,0 -16,16.6c-0.2,0.2 -0.4,0.3 -0.7,0.3 -0.3,0 -0.5,-0.1 -0.7,-0.3l-7.8,-8.4 -0.2,-0.3z"/>
</vector>

我认为问题出在您的 'button' 属性上。我很幸运:

   <ToggleButton
            android:id="@+id/contacts_subscribe_button"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="@drawable/checkbox_bg"
            android:src="@drawable/checkbox_src"
            android:checked="true"
            android:gravity="center"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:textOff="@null"
            android:textOn="@null" />

checkbox_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@color/transparent" />
<item android:state_checked="true" android:drawable="@drawable/ic_checked" />
<item android:drawable="@color/transparent" />

checkbox_src.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/brikky1"/>
<corners
    android:radius="2dp"
    android:bottomLeftRadius="2dp"
    android:bottomRightRadius="2dp"
    android:topLeftRadius="2dp"
    android:topRightRadius="2dp" />
<size android:height="32dp" android:width="32dp"/>
</shape>