android自定义的RadioButton

自定义的RadioButton:可以调节选中框与字体的距离,可以设置选中框的位置

效果如下:


一.在strings.xml里加入下列代码

<declare-styleable name="RadioButton_draw">
        <attr name="drawableSize" format="dimension" />
        <attr name="drawableTop" format="reference" />
        <attr name="drawableLeft" format="reference" />
        <attr name="drawableRight" format="reference" />
        <attr name="drawableBottom" format="reference" />
    </declare-styleable>

二、自定义的RadioButton

package com.example.customradiobutton;

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


/*
* 描述:RadioButton  图片大小
* 文件名:RadioButtonDrawSize.java7
*/
public class RadioButtonDrawSize extends RadioButton {

    private int mDrawableSize;// xml文件中设置的大小  

    public RadioButtonDrawSize(Context context) {
        this(context, null, 0);
    }

    public RadioButtonDrawSize(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadioButtonDrawSize(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadioButton_draw);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.RadioButton_draw_drawableSize) {
                mDrawableSize = a.getDimensionPixelSize(R.styleable.RadioButton_draw_drawableSize, 50);

            } else if (attr == R.styleable.RadioButton_draw_drawableTop) {
                drawableTop = a.getDrawable(attr);

            } else if (attr == R.styleable.RadioButton_draw_drawableBottom) {
                drawableRight = a.getDrawable(attr);

            } else if (attr == R.styleable.RadioButton_draw_drawableRight) {
                drawableBottom = a.getDrawable(attr);

            } else if (attr == R.styleable.RadioButton_draw_drawableLeft) {
                drawableLeft = a.getDrawable(attr);

            } else {
            }
        }
        a.recycle();
        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }

    /**
     * RadioButton上、下、左、右设置图标
     */
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (right != null) {
            right.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (top != null) {
            top.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        setCompoundDrawables(left, top, right, bottom);
    }
}  

三、在activity_main.xml使用

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.customradiobutton.MainActivity">

    <com.example.customradiobutton.RadioButtonDrawSize
        android:id="@+id/radioButton"
        style="@style/inform_dialog_RadioButtonDrawSize"
        android:layout_margin="10dp"
        android:clickable="true"
        android:checked="true"
        android:text="自定义的button1"
        app:drawableLeft="@drawable/jubao_check"
        app:drawableSize="30dp" />
    <com.example.customradiobutton.RadioButtonDrawSize
        android:id="@+id/radioButton2"
        style="@style/inform_dialog_RadioButtonDrawSize"
        android:layout_margin="10dp"
        android:clickable="true"
        android:text="自定义的button2"
        app:drawableLeft="@drawable/jubao_check"
        app:drawableSize="30dp" />

    <com.example.customradiobutton.RadioButtonDrawSize
        android:id="@+id/radioButton3"
        style="@style/inform_dialog_RadioButtonDrawSize"
        android:layout_margin="10dp"
        android:text="自定义的button3"
        android:clickable="true"
        app:drawableLeft="@drawable/jubao_check"
        app:drawableSize="30dp" />

</RadioGroup>


项目地址:http://download.csdn.net/download/zhang106209/10040731


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值