动态设置Button图片大小

本文介绍如何在Android开发中动态设置Button的大小及处理点击事件。通过自定义一个名为MyRadioButton的类,继承RadioButton,并能根据需求调整字体颜色。在主布局文件中,可以仿照腾讯底部导航进行设计,接下来将实现顶部导航。
摘要由CSDN通过智能技术生成

动态设置Button大小及点击事件:

在布局中写下这段代码:新创建一个class 名字叫MyRadioButton 继承RdioButton

package duan.com.homework.helper;

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

import duan.com.homework.R;


/**
 * Created by Administrator on 2016/9/22.
 */
public class MyRadioButton extends RadioButton {

    public MyRadioButton(Context context) {
        super(context);
    }

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyRadioButton);
        //drawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_rbDrawableTopSize, 50);
        Drawable drawableTop = a.getDrawable(R.styleable.MyRadioButton_rbDrawableTop);
        a.recycle();
        setCompoundDrawablesWithIntrinsicBounds(null,drawableTop,null,null);
    }



    public MyRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

        if (top!=null){
            top.setBounds(0,0,50,50);
        }
        setCompoundDrawables(left,top,right,bottom);
    }
}
然后在res下创建attrs:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyRadioButton">
        <attr name="rbDrawableTopSize" format="dimension"/>
        <attr name="rbDrawableTop" format="reference"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值