android自定义的RadioButton
来源:互联网 发布:大数据时代来临 编辑:程序博客网 时间:2024/05/16 10:58
自定义的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 0
- Android RadioButton的自定义
- android自定义的RadioButton
- Android 自定义RadioButton的样式
- Android 自定义RadioButton的样式
- Android 自定义RadioButton的样式
- Android 自定义RadioButton的样式
- Android 自定义RadioButton的样式
- RadioButton - android 自定义RadioButton样式
- android的自定义样式的radiobutton
- Android之自定义RadioButton的样式
- android 自定义RadioButton样式
- android 自定义RadioButton样式
- Android 自定义RadioButton样式
- Android自定义RadioButton
- Android自定义RadioButton样式
- android 自定义RadioButton样式
- android 自定义RadioButton样式
- android 自定义RadioButton样式
- 深入理解C++中public、protected及private用法
- 字符串匹配问题
- 最长重复词问题(java)
- 百度天工IoT 快速上手
- 使用jquery实现文字滚动,鼠标悬停效果
- android自定义的RadioButton
- Android线程优先级
- 在Android手机上运行Windows系统
- Java中的三种代理模式
- linux 下shell 脚本编程之退出码
- pandas.to_datetime
- HTTP协议状态码详解(HTTP Status Code)
- (自用的)windows 任务管理器 API 调用
- 利用纯css实现基本图形