动态设置Button图片大小

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态设置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"/>
    </declare-styleable>
</resources>
这是一个按钮 点击切换图标  我就写了一个 其他自己写就OK 其实很简单

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/timeline_toolbar_btn_news_selected"></item>
    <item android:drawable="@drawable/timeline_toolbar_btn_news_normal" android:state_checked="false"></item>
</selector>

 字体颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#168DFE"></item>
    <item android:state_checked="false" android:color="#777777"></item>
</selector>

然后在主布局中,我的主布局就是RadioGroup中套 RadioButton

<RelativeLayout 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"
    tools:context=".MainActivity">
 上面的app和tools一定要写出来 这个就自动生成就好了,然后在Button按钮中写

<RadioGroup
    android:id="@+id/radioGroup_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    // 横向一定要写 不然会没有东西
    android:orientation="horizontal">

    <duan.com.homework.helper.MyRadioButton
        android:id="@+id/button_news"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        //这步就是去掉Button前面的圆圈 
        android:button="@null"
        //选中的图片 默认选中 因为是第一个按钮
        android:checked="true"
        //让文字居中 在Button的下面
        android:gravity="center"
        android:text="新闻"
        //这步最重要 不能自动生成 需要手动来写 就是把之前写的attrs中的rbDrawableTop拿来使用 
	后面就是drawable资源里面的选中和非选中 按钮的切换
        app:rbDrawableTop="@drawable/button_color_news" />
是不是图片变小了  

如果不是你想要的 你还可以在MyRadioButton中修改  


下面就是我主布局里面的代码  我是模仿腾讯底部做的  下一步做顶部导航 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <duan.com.homework.helper.MyRadioButton
            android:id="@+id/button_news"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="新闻"
            android:textColor="@drawable/button_text_color"
            app:rbDrawableTop="@drawable/button_color_news" />

        <duan.com.homework.helper.MyRadioButton
            android:id="@+id/button_recommend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:gravity="center"
            android:text="推荐"
            android:textColor="@drawable/button_text_color"
            app:rbDrawableTop="@drawable/button_color_recommend" />

        <duan.com.homework.helper.MyRadioButton
            android:id="@+id/button_live"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:gravity="center"
            android:text="直播"
            android:textColor="@drawable/button_text_color"
            app:rbDrawableTop="@drawable/button_color_live" />

        <duan.com.homework.helper.MyRadioButton
            android:id="@+id/button_me"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:gravity="center"
            android:text=""
            android:textColor="@drawable/button_text_color"
            app:rbDrawableTop="@drawable/button_color_me" />

    </RadioGroup>
	
	<FrameLayout
   	 android:id="@+id/frame"
    	 android:layout_width="match_parent"
   	 android:layout_height="match_parent"
    	 android:layout_above="@id/radioGroup_main"
  	  ></FrameLayout>
</RelativeLayout>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值