赞
踩
- <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>
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); } }
- <?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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。