Android自定义控件-组合控件学习-

public class MPNavBar extends LinearLayout{
	
	public static final int ID_NAV_ALBUM = 0;
	public static final int ID_NAV_AUDIO = 1;
	public static final int ID_NAV_FOLDER = 2;
	public static final int ID_NAV_SETTING = 3;
	public static final int ID_NAV_MAX = 4;
	//当前选中的按钮
	private int mNow = -1;
	//button的容器
	private RadioButton toggles[] = new RadioButton[ID_NAV_MAX];
	
	//声明对外的接口
	private OnChangedListener mListener;
	
	public MPNavBar(Context context) {
		this(context,null);
		
	}
	public MPNavBar(Context context,AttributeSet attrs){
		this(context,attrs,0);
	}
	public void setActive(int id){
		if(id >= 0 && id < ID_NAV_MAX){
			TouchEventHelper.dispatchClickEvent(toggles[id]);
		}
	}
	public MPNavBar(Context context,AttributeSet attrs,int defStyle){
		super(context,attrs,0);
		//水平放置子布局
		super.setOrientation(LinearLayout.HORIZONTAL);
		//设置子布局垂直居中
		super.setGravity(Gravity.CENTER_VERTICAL);
		

		toggles[ID_NAV_ALBUM] = newRadioButton(context, ID_NAV_ALBUM, R.drawable.ic_nav_album, R.string.nav_album);
		toggles[ID_NAV_AUDIO] = newRadioButton(context, ID_NAV_AUDIO, R.drawable.ic_nav_audio, R.string.nav_audio);
		toggles[ID_NAV_FOLDER] = newRadioButton(context, ID_NAV_FOLDER, R.drawable.ic_nav_folder, R.string.nav_folder);
		toggles[ID_NAV_SETTING] = newRadioButton(context, ID_NAV_SETTING, R.drawable.ic_nav_setting, R.string.nav_setting);
		
		super.addView(newSplit(context));
		super.addView(toggles[ID_NAV_ALBUM]);
		
		super.addView(newSplit(context));
		//super.addView(toggles[ID_NAV_AUDIO]);
		//super.addView(newSplit(context));
		super.addView(toggles[ID_NAV_FOLDER]);
		super.addView(newSplit(context));
		
		super.addView(toggles[ID_NAV_SETTING]);
		super.addView(newSplit(context));
	}
	
	//创建一个View 用于分割
	private View newSplit(Context context){
		View split = new View(context);
		split.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
		return split;
	}
	
	private RadioButton newRadioButton(Context context,int resID,int resTop,int resText){
		//获得资源
		final Resources resources = context.getResources();
		//获取屏幕的尺寸大小
		final DisplayMetrics dm = resources.getDisplayMetrics();
		//转化为标准尺寸
		final int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, dm);
		final int top = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, dm);
		final int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, dm);
		final int bottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, dm);
		
		//初始化字体大小
		int textSize = 0;
		//检查是否有新的代码块生成
		if(VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB){
			textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 9, dm);
		}else{
			textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 9, dm);
		}
		
		//创建radioButton
		RadioButton radioButton = new RadioButton(context);
		//给radioButton设置ID和资源参数
		radioButton.setId(resID);
		radioButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
		radioButton.setText(resText);
		radioButton.setTextSize(textSize);
		radioButton.setButtonDrawable(resources.getDrawable(android.R.color.transparent));
		//设置图片资源
		radioButton.setCompoundDrawablesWithIntrinsicBounds(0, resTop, 0, 0);
		radioButton.setPadding(left, top, right, bottom);
		//设置事件监听
		radioButton.setOnClickListener(mOnClickListener);
		//按钮按下和默认的颜色
		int[] colors = new int[]{0xFF0079FF, 0xFF888888};
		int[][] state = new int[2][];
		//按钮被点击是的状态
		state[0] = new int[]{android.R.attr.checked};
		//按钮正常是的状态
		state[1] = new int[]{};
		//设置字体的颜色
		radioButton.setTextColor(new ColorStateList(state, colors));
		return radioButton;
	}
	//给按钮设置时间监听   当按下时发生时要执行的动作
	private View.OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			//获取按钮的ID
			int id = v.getId();
			//如果点击的不是当前的控件
			if( mNow != id){
				//遍历所有的控件
				for(int i = 0;i < ID_NAV_MAX;i++){
					toggles[i].setChecked(id == i? true:false);
					
				}
				mNow = id;
				//调用onChanged方法
				if(mListener != null){
					mListener.onChanged(MPNavBar.this, mNow);
				}
				
			}
			
		}
	};
	
	//创建对外接口  当用户点击的时候明确哪个按钮被点击了
	public interface OnChangedListener{
		void onChanged(MPNavBar bar, int id);
	}
	
	//创建对外的方法  提供给用户使用
	public void setOnChangedListener(OnChangedListener listener){
		this.mListener = listener;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值