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);
}
}
TitleButton
最新推荐文章于 2021-07-16 10:43:38 发布