使用中心图像制作自定义单选按钮 [英] Making Custom RadioButton with image on center

查看:73
本文介绍了使用中心图像制作自定义单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的自定义RadioButton看起来像这样:

I'm trying to my custom RadioButton that I trying to make to look like this:

因此,我做了一些自定义可绘制对象,它们响应RadioButton状态并将其用作背景.没关系.

So I did custom drawables that respond to the RadioButton status and used it as background. This is alright.

问题是我无法通过 android:button 属性设置的图像居中.

The problem is that I'm not been able to center the images that I set through the android:button atribute.

这是我尝试在布局中使用它的方式.

Here is how I'm trying to use it in my layout.

<RadioGroup
        android:id="@+id/presenter_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dp"
        android:gravity="center"
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/cluster_centered"
            android:gravity="center"
            android:padding="8dp"
            android:background="@drawable/presenter_radiobutton_left"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="8dp"
            android:background="@drawable/presenter_radiobutton_right"
            android:button="@drawable/fire"
            />

</RadioGroup>

有了这个,我得到了这个结果:

With this I'm getting this as result:

我已经尝试定义一个将重力设置为中心的可绘制对象,但未进行任何更改.这是自定义可绘制对象:

I've already tried to define a drawable that sets the gravity to center, but nothing changed. Here is the custom drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/cluster" android:gravity="center"/>
    </item>
</selector>

那么,如何才能将按钮图像居中?

So, how could I center the button images?

PS:我不能将每种按钮设置为背景,因为它将来会动态变化,因此唯一可能在背景中的是蓝色形状.

PS: I can't set each kind of button as background because it'll be dynamic in the future, so the only thing that could be in background is the blue shape.

推荐答案

我的解决方案是设置 android:button = @ null ,然后将所需的图像设置为 android:drawableLeft 属性.所以我的RadioButton代码是这样的:

My solution was to set android:button=@null, then set the image that I want into the android:drawableLeft attribute. So my RadioButton code is like this:

<RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="8dp"
            android:background="@drawable/presenter_radiobutton_left"
            android:button="@null"
            android:drawableLeft="@drawable/fire"
            />

这篇关于使用中心图像制作自定义单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆