TitleButton

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TitleButton extends LinearLayout {

	public TitleButton(Context context, AttributeSet attr) {
		super(context, attr);
		init(context, attr);
	}

	private void init(Context context, AttributeSet attr) {
		final ImageView iv = new ImageView(context);
		LayoutParams imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		iv.setLayoutParams(imageParams);
		iv.setBackgroundResource(R.drawable.title_img_normal);
		addView(iv);

		final TextView tv = new TextView(context, attr);
		LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		textParams.setMargins(12, 0, 0, 0);
		tv.setLayoutParams(textParams);
		tv.setTextSize(40);
		tv.setTextColor(Color.WHITE);
		tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		addView(tv);

		OnTouchListener impassiveListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		};
		iv.setOnTouchListener(impassiveListener);
		tv.setOnTouchListener(impassiveListener);

		this.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					iv.setBackgroundResource(R.drawable.title_img_pressed);
					tv.setTextColor(Color.rgb(255, 114, 0));
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
					iv.setBackgroundResource(R.drawable.title_img_normal);
					tv.setTextColor(Color.WHITE);
					break;
				default:
					break;
				}
				return false;
			}
		});
		String clickAttr = attr.getAttributeValue("http://schemas.android.com/apk/res/android", "onClick");
		if (clickAttr == null || clickAttr.length() == 0) {
			setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Activity activity = (Activity) getContext();
					activity.finish();
				}
			});
		}

		setBackgroundColor(Color.TRANSPARENT);
		setPadding(23, 10, 0, 10);
		setGravity(Gravity.CENTER_VERTICAL);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值