RadioButton的图标改变大小(TextView也适用)

时间:2022-09-08 07:22:14

RadioButton的图标大小并没有相应的布局参数,本文通过自定义属性的方式自定义RadioButton,实现控制图片大小。

  • 本文要点:
  1. 自定义属性的使用。
  2. 解决RadioButton文字上、下、左、右的图标大小自定义问题。
  3. 此方法对TextView内的图标也适用。
  • 问题如下:

RadioButton的图标改变大小(TextView也适用)

  • 解决方法:

1.values/attrs.xml 文件中:自定义rb_width  和  rb_height  两个属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyRadioButton">
<attr name="rb_width" format="dimension"/>
<attr name="rb_height" format="dimension"/>
</declare-styleable>
</resources>

2.自定义RadioButton

 package top.toly.www.myqq.view;

 import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton; import top.toly.www.myqq.R;
import utils.shortUtils.Change; /**
* 作者:张风捷特烈
* 时间:2018/3/28:6:30
* 邮箱:1981462002@qq.com
* 说明:自定义RadioButton 属性:rb_width rb_height
*/
public class MyRadioButton extends RadioButton { private float mImg_width;
private float mImg_height; public MyRadioButton(Context context) {
super(context);
} public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
mImg_width = t.getDimension(R.styleable.MyRadioButton_rb_width, Change.dp2px());
mImg_height = t.getDimension(R.styleable.MyRadioButton_rb_height, Change.dp2px());
t.recycle();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//让RadioButton的图标可调大小 属性:
Drawable drawableLeft = this.getCompoundDrawables()[];//获得文字左侧图片
Drawable drawableTop = this.getCompoundDrawables()[];//获得文字顶部图片
Drawable drawableRight = this.getCompoundDrawables()[];//获得文字右侧图片
Drawable drawableBottom = this.getCompoundDrawables()[];//获得文字底部图片
if (drawableLeft != null) {
drawableLeft.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(drawableLeft, null, null, null);
}
if (drawableRight != null) {
drawableRight.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(null, null, drawableRight, null);
}
if (drawableTop != null) {
drawableTop.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(null, drawableTop, null, null);
}
if (drawableBottom != null) {
drawableBottom.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(null, null, null, drawableBottom);
}
}
}

3.使用属性控制RadioButton 中的图片大小

  • 注意:
  1. <top.toly.www.myqq.view.MyRadioButton 为自定义控件类的全路径名
  2.  xmlns:toly="http://schemas.android.com/apk/res-auto"  声明命名空间,其中toly为自定义名称,可替换(需与第3点冒号前名称一致)
  3.  toly:rb_width="30dp"  toly:rb_height="30dp" 为自定义属性的使用
  4.  android:drawableTop="@drawable/tab_msg_selector" 为指定的图片资源
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toly="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/qq_title_text"> <RadioGroup
android:id="@+id/rg_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"> <top.toly.www.myqq.view.MyRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:id="@+id/rb_msg"
android:checked="false"
toly:rb_width="30dp"
toly:rb_height="30dp"
android:button="@null"
android:gravity="center"
android:drawableTop="@drawable/tab_msg_selector"
android:text="消息"/> <top.toly.www.myqq.view.MyRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:id="@+id/rb_contact"
android:background="@android:color/transparent"
android:checked="false"
android:button="@null"
toly:rb_width="30dp"
toly:rb_height="30dp"
android:gravity="center"
android:drawableTop="@drawable/tab_contact_selector"
android:text="联系人"/> <top.toly.www.myqq.view.MyRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:id="@+id/rb_act"
android:button="@null"
toly:rb_width="30dp"
toly:rb_height="30dp"
android:gravity="center"
android:background="@android:color/transparent"
android:drawableTop="@drawable/tab_act_selector"
android:text="动态"
/>
</RadioGroup>
</RelativeLayout>

4.结果图:

RadioButton的图标改变大小(TextView也适用)