Android的单选按钮图像 [英] Android radiobutton image

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

问题描述

我有一个自定义的单选按钮一个RadioGroup中。图标使用设置

  rbFirst.setButtonDrawable(R.drawable.first);
 

但图标是不是在市中心,我该如何解决? 我试图在XML文件中,内线等级不同的属性,但它没有任何效果。

解决方案

尝试使用 setGravity(Gravity.CENTER); 将其设置为背景绘制的。

或者,如果它不能帮助你将不得不从单选派生新类和重写的OnDraw()。

下面的布局

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
< org.test.TestProj.RadioButtonCenter
    机器人:ID =@ + ID / MyView的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =100dp
    机器人:layout_centerInParent =真
    机器人:文本=按钮测试
    />
< / RelativeLayout的>
 

在code:

 包org.test.TestProj;

进口android.content.Context;
进口android.content.res.TypedArray;
进口android.util.AttributeSet;
进口android.view.Gravity;
进口android.widget.RadioButton;
进口android.graphics.Canvas;
进口android.graphics.drawable.Drawable;

公共类RadioButtonCenter扩展单选{

    公共RadioButtonCenter(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        TypedArray一个= context.obtainStyledAttributes(ATTRS,R.styleable.CompoundButton,0,0);
        buttonDrawable = a.getDrawable(1);
        setButtonDrawable(android.R.color.transparent);
    }
    绘制对象buttonDrawable;


     @覆盖
        保护无效的OnDraw(帆布油画){
            super.onDraw(画布);

            如果(buttonDrawable!= NULL){
                buttonDrawable.setState(getDrawableState());
                最终诠释verticalGravity = getGravity()及Gravity.VERTICAL_GRAVITY_MASK;
                最终诠释身高= buttonDrawable.getIntrinsicHeight();

                INT Y = 0;

                开关(verticalGravity){
                    案例Gravity.BOTTOM:
                        Y =的getHeight() - 高度;
                        打破;
                    案例Gravity.CENTER_VERTICAL:
                        Y =(的getHeight() - 高度)/ 2;
                        打破;
                }

            INT buttonWidth = buttonDrawable.getIntrinsicWidth();
            INT buttonLeft =(的getWidth() -  buttonWidth)/ 2;
            buttonDrawable.setBounds(buttonLeft,Y,buttonLeft + buttonWidth,Y +高);
                buttonDrawable.draw(画布);
            }
        }
}
 

最后,这里是你需要把RES /值,因此code可以在平台定义的属性得到attrs.xml文件。

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
     <申报,设置样式名称=CompoundButton>
        < attr指示NAME =安卓键/>
    < /申报,设置样式>
< /资源>
 

I have a radiogroup with custom radiobuttons. Icons are set using

rbFirst.setButtonDrawable(R.drawable.first);

But icon is not in the center, how do I fix it? I tried different attributes in xml file and inside class but it had no effect.

解决方案

Try using setGravity(Gravity.CENTER); of setting it as a background drawable.

Or if it does not help you will have to derive a new class from RadioButton and override onDraw().

Here's layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<org.test.TestProj.RadioButtonCenter
    android:id="@+id/myview"
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    android:layout_centerInParent="true"
    android:text="Button test"
    />
</RelativeLayout>

The code:

package org.test.TestProj;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

public class RadioButtonCenter extends RadioButton {

    public RadioButtonCenter(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0);
        buttonDrawable = a.getDrawable(1);
        setButtonDrawable(android.R.color.transparent);
    }
    Drawable buttonDrawable;


     @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (buttonDrawable != null) {
                buttonDrawable.setState(getDrawableState());
                final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
                final int height = buttonDrawable.getIntrinsicHeight();

                int y = 0;

                switch (verticalGravity) {
                    case Gravity.BOTTOM:
                        y = getHeight() - height;
                        break;
                    case Gravity.CENTER_VERTICAL:
                        y = (getHeight() - height) / 2;
                        break;
                }

            int buttonWidth = buttonDrawable.getIntrinsicWidth();
            int buttonLeft = (getWidth() - buttonWidth) / 2;
            buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height);
                buttonDrawable.draw(canvas);
            }
        }   
}

Finally, here's an attrs.xml file you need to put in res/values so the code can get at platform-defined attributes.

<?xml version="1.0" encoding="utf-8"?>
<resources>    
     <declare-styleable name="CompoundButton">
        <attr name="android:button" />
    </declare-styleable>
</resources>

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

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