动态设置Button图片大小

动态设置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"?>

    name="MyRadioButton">
        name="rbDrawableTopSize" format="dimension"/>
        name="rbDrawableTop" format="reference"/>
    
这是一个按钮 点击切换图标  我就写了一个 其他自己写就OK 其实很简单

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

 字体颜色

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

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

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按钮中写

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

            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"?>
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">

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

                    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" />

                    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" />

                    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" />

                    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" />

    
	
	   	 android:id="@+id/frame"
    	 android:layout_width="match_parent"
   	 android:layout_height="match_parent"
    	 android:layout_above="@id/radioGroup_main"
  	  >





你可能感兴趣的:(Button图片大小切换)