1. 程式人生 > >Android自定義元件之美化radiobutton

Android自定義元件之美化radiobutton

其實美化方法很簡單隻需重寫ondraw方法是替換圖片即可

下面是原始碼

package com.myradio;


import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.RadioButton;


public class MyRadioButton extends RadioButton {


	public MyRadioButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		//LayoutInflater.from(context).inflate(R.layout.my_layout, this, true);
		
	}
	public MyRadioButton(Context context) {
		super(context);
		//LayoutInflater.from(context).inflate(R.layout.my_layout, this, true);
	}
	@Override
	public boolean isChecked() {
		return super.isChecked();
	}
	public void setButtonDrawable(int resid) {
		super.setButtonDrawable(resid);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		
		if(this.isChecked())
		{
			this.setButtonDrawable(R.drawable.circle_green);
		}
		else
		{
			this.setButtonDrawable(R.drawable.circle_grey);
		}
		super.onDraw(canvas);
	}
}

附上圖片: