简体   繁体   中英

Copying android:drawableStart for RadioButtons in API 8

I need a custom radio button with a custom background, centered text, an icon immediately before the text, but without the default indicator.

Currently, I have the following code:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:background="@drawable/bg_rbtn_custom"
    android:drawableLeft="@drawable/icon"
    android:text="Lorem ipsum…" />

The problem is the icon defined in drawableLeft , is pushed all the way to the left, ie

[icon]____Lorem ipsum…________

I need this:

____[icon]Lorem ipsum…________

The same thing happens when I use android:button="@drawable/icon ; the icon is place at the left-most part of the View , and the text is then centred inside the left-over space (rather than being centred relative to the entire View ). I am Android API 8, so I can't use drawableStart , so I need a way to duplicate its behaviour (at least I assume that's what it does). The text is dynamic, and will change at runtime, so I can't really hard code the padding.

My question is quite similar to this one , but that guy only needed a Button , but I need a RadioButton that'll work in a RadioGroup .

First thoughts would be to try

android:paddingLeft="#db"

where "#db" would be the number of pixels it would take to center everything


After trying your code and replacing the background and icon to local resources I had, I was unable to recreate the issue you are having. Could it be possible that the icon you are using has transparent pixels on its right edge?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM