Center CheckBox drawable within itself

2020-05-19 04:31发布

I have a CheckBox that I want centered within its own boundaries, not pushed to the side. Probably easier demonstrated than explained:

enter image description here

Note that it isn't centered. Currently defined as:

<CheckBox
    android:id="@+id/checkbox_star"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:button="@drawable/btn_favorite"

    android:layout_gravity="center"
    android:minWidth="48dp" />

Nevermind the custom button drawable. It behaves the same with a vanilla CheckBox as well (the small check box behaves the same).

12条回答
霸刀☆藐视天下
2楼-- · 2020-05-19 04:55

The only proper solution is to add insets to the image. Most of the solutions with "foreground" won't work below API 23.

btn_favorite_inset.xml in drawable dir

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/btn_favorite"
    android:insetLeft="6dp"
    android:insetTop="6dp"
    android:insetRight="6dp"
    android:insetBottom="6dp" />

Your layout file

<CheckBox
    android:id="@+id/checkbox_star"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:button="@drawable/btn_favorite_inset"
    android:layout_gravity="center"
    android:minWidth="48dp" />
查看更多
我命由我不由天
3楼-- · 2020-05-19 04:56

Do not use this code to bypass checkbox bug with center aligment for >SDK23 if you are going to use checkboxes in recyclerview.

<CheckBox
android:button="@null"
android:drawableLeft="@drawable/checkbox_selector"
android:drawableStart="@drawable/checkbox_selector"
/>

Checkboxes will not get checked in onBindViewHolder until next refresh if RV (example: scroll,..).

查看更多
冷血范
4楼-- · 2020-05-19 05:00
android:button="@null"
android:foreground="@drawable/btn_favorite" 
android:foregroundGravity="center"
查看更多
相关推荐>>
5楼-- · 2020-05-19 05:01

In your code, you have set the height of checkbox to match_parent. Set it to wrap_content. This will solve your problem. Worked for me!

Problem:

<CheckBox
    android:id="@+id/checkbox_star"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:button="@drawable/btn_favorite"

    android:layout_gravity="center"
    android:minWidth="48dp" />

Solution:

<CheckBox
    android:id="@+id/checkbox_star"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/btn_favorite"

    android:layout_gravity="center"
    android:minWidth="48dp" />
查看更多
别忘想泡老子
6楼-- · 2020-05-19 05:05

Set the android:width so that it only shows your checkbox, then center it using android:layout_gravity="center".

查看更多
放荡不羁爱自由
7楼-- · 2020-05-19 05:07

I have tried j__m's answer above, and it works, but not good enough.

In my case, I wanted to add paddings around the CheckBox's drawable, in order to increase the touch area. And j__m's answer makes it hard to adjust the UI.

I think a better solution is to create an inset drawable to wrap your original state drawable, which should be very easy and work perfectly.

查看更多
登录 后发表回答