Android 自定义ViewGroup控件

实现一个简单的组合ViewGroup控件TopBar

首先要在values目录下创建一个attrs.xml文件,设定一些自定义控件的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Topbar">
        <!--format表示将来所要引用的资源类型-->
        <attr name="titleTmp" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColorTmp" format="color"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <!--背景不仅可以是颜色代码也可以引用文件-->
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>

然后创建一个自定义控件继承ViewGroup,这里直接继承RelativeLayout

public class TopBar extends RelativeLayout {
	private Button leftButton, rightButton;
	private TextView tvTitile;

	private int leftTextColor;
	private Drawable leftBackground;
	private String leftText;

	private int rightTextColor;
	private Drawable rightBackground;
	private String rightText;

	private float titleTextSize;
	private int titleTextColor;
	private String title;

	private LayoutParams leftParams, rightParams, titleParams;

	private topbarClickListener listener;

	public interface topbarClickListener {
		public void leftClick();

		public void rightClick();
	}

	public void setOnTopBarClickListener(topbarClickListener listener) {
		this.listener = listener;
	}

	@SuppressLint("NewApi")
	public TopBar(final Context context, AttributeSet attrs) {
		super(context, attrs);
		// 通过TypedArray获取在xml中自定义的属性值,ta中包含所有属性值和映射
		// TypedArray类似于map,通过键值对存储变量
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.Topbar);

		leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
		leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
		leftText = ta.getString(R.styleable.Topbar_leftText);

		rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
		rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
		rightText = ta.getString(R.styleable.Topbar_rightText);

		titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
		titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColorTmp, 0);
		title = ta.getString(R.styleable.Topbar_titleTmp);

		// 避免浪费资源和以及缓存造成的错误
		ta.recycle();

		leftButton = new Button(context);
		rightButton = new Button(context);
		tvTitile = new TextView(context);

		leftButton.setTextColor(leftTextColor);
		leftButton.setBackground(leftBackground);
		leftButton.setText(leftText);

		rightButton.setTextColor(rightTextColor);
		rightButton.setBackground(rightBackground);
		rightButton.setText(rightText);

		tvTitile.setTextColor(titleTextColor);
		tvTitile.setTextSize(titleTextSize);
		tvTitile.setText(title);
		tvTitile.setGravity(Gravity.CENTER);

		setBackgroundColor(0xFFF59563);

		// 设置左边控件的一些属性
		leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		// 此时的true是一个参数,不是bool
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
		rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
		titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);

		addView(leftButton, leftParams);
		addView(rightButton, rightParams);
		addView(tvTitile, titleParams);

		leftButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.leftClick();
			}
		});

		rightButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.rightClick();
			}
		});
	}

	/**
	 * 设置左边按钮是否显示
	 * 
	 * @param flag
	 */
	public void setLeftIsVisable(Boolean flag) {
		if (flag) {
			leftButton.setVisibility(View.VISIBLE);
		} else {
			leftButton.setVisibility(View.INVISIBLE);
		}
	}
}

在Activity中就可以像普通控件一样findViewById,同时可以设定左右两个按钮的响应事件

        TopBar topBar = (TopBar)findViewById(R.id.topbar);
        topBar.setOnTopBarClickListener(new TopBar.topbarClickListener(){
            @Override
            public void leftClick() {
                Toast.makeText(MainActivity.this,"Left Button",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void rightClick() {
                Toast.makeText(MainActivity.this,"Right Button",Toast.LENGTH_SHORT).show();
            }
        });


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值