自定义RadioButton:改变文字上、下、左、右图片,可设置图片大小(宽高)

原文出处:https://www.cnblogs.com/toly/p/8665926.html

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

  • 本文要点:
  1. 自定义属性的使用。
  2. 解决RadioButton文字上、下、左、右的图标大小自定义问题。
  3. 此方法对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

复制代码

 1 package top.toly.www.myqq.view;
 2 
 3 import android.content.Context;
 4 import android.content.res.TypedArray;
 5 import android.graphics.drawable.Drawable;
 6 import android.util.AttributeSet;
 7 import android.widget.RadioButton;
 8 
 9 import top.toly.www.myqq.R;
10 import utils.shortUtils.Change;
11 
18 public class MyRadioButton extends RadioButton {
19 
20     private float mImg_width;
21     private float mImg_height;
22 
23     public MyRadioButton(Context context) {
24         super(context);
25     }
26 
27     public MyRadioButton(Context context, AttributeSet attrs) {
28         super(context, attrs);
29         TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
30         mImg_width = t.getDimension(R.styleable.MyRadioButton_rb_width, Change.dp2px(25));
31         mImg_height = t.getDimension(R.styleable.MyRadioButton_rb_height, Change.dp2px(25));
32         t.recycle();
33     }
34 
35     @Override
36     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
37         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
38         //让RadioButton的图标可调大小 属性:
39         Drawable drawableLeft = this.getCompoundDrawables()[0];//获得文字左侧图片
40         Drawable drawableTop = this.getCompoundDrawables()[1];//获得文字顶部图片
41         Drawable drawableRight = this.getCompoundDrawables()[2];//获得文字右侧图片
42         Drawable drawableBottom = this.getCompoundDrawables()[3];//获得文字底部图片
43         if (drawableLeft != null) {
44             drawableLeft.setBounds(0, 0, (int) mImg_width, (int) mImg_height);
45             this.setCompoundDrawables(drawableLeft, null, null, null);
46         }
47         if (drawableRight != null) {
48             drawableRight.setBounds(0, 0, (int) mImg_width, (int) mImg_height);
49             this.setCompoundDrawables(null, null, drawableRight, null);
50         }
51         if (drawableTop != null) {
52             drawableTop.setBounds(0, 0, (int) mImg_width, (int) mImg_height);
53             this.setCompoundDrawables(null, drawableTop, null, null);
54         }
55         if (drawableBottom != null) {
56             drawableBottom.setBounds(0, 0, (int) mImg_width, (int) mImg_height);
57             this.setCompoundDrawables(null, null, null, drawableBottom);
58         }
59     }
60 }

复制代码

 

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="1"
            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="1"
            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="1"
            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.结果图:

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值