简体   繁体   English

为API 8中的RadioButtons复制android:drawableStart

[英]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 问题是在drawableLeft定义的图标一直推到最左边,即

[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. 当我使用android:button="@drawable/icon ,会发生同样的事情;图标位于View的最左侧,然后将文本居中于剩余空间内(而不是相对于整个View )。我使用的是Android API 8,因此无法使用drawableStart ,因此我需要一种方法来复制其行为(至少我认为是这样做的)。文本是动态的,并且在运行时更改,因此我无法对填充进行硬编码。

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 . 我的问题与这个问题非常相似,但是那个人只需要一个Button ,但是我需要一个在RadioGroup工作的RadioButton

First thoughts would be to try 首先想到的是尝试

android:paddingLeft="#db"

where "#db" would be the number of pixels it would take to center everything 其中“ #db”是将所有内容居中所需的像素数


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? 您使用的图标的右边缘是否可能有透明像素?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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