1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| public class CustomToolBar extends RelativeLayout {
private ImageView leftImg, rightImg; private TextView titleTextView;
public interface ImgClickListener{ public void leftImgClick(); public void rightImgClick(); } private ImgClickListener imgClickListener;
public void setImgClickListener(ImgClickListener imgClickListener) { this.imgClickListener = imgClickListener; }
public CustomToolBar(Context context) { this(context, null); }
public CustomToolBar(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public CustomToolBar(final Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); titleTextView = new TextView(context); leftImg = new ImageView(context); leftImg.setPadding(12, 12, 12, 12); rightImg = new ImageView(context); rightImg.setPadding(12, 12, 12, 12); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomToolBar); String titleText = ta.getString(R.styleable.CustomToolBar_titleText); int titleTextColor = ta.getColor(R.styleable.CustomToolBar_myTitleTextColor, Color.BLACK); float titleTextSize = ta.getDimension(R.styleable.CustomToolBar_titleTextSize, 12);
Drawable leftDrawable = ta.getDrawable(R.styleable.CustomToolBar_leftImageSrc); Drawable rightDrawable = ta.getDrawable(R.styleable.CustomToolBar_rightImageSrc);
ta.recycle();
leftImg.setImageDrawable(leftDrawable); rightImg.setImageDrawable(rightDrawable); titleTextView.setText(titleText); titleTextView.setTextSize(titleTextSize); titleTextView.setTextColor(titleTextColor);
LayoutParams leftParams = new LayoutParams((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics())); leftParams.addRule(ALIGN_PARENT_LEFT, TRUE); this.addView(leftImg, leftParams);
LayoutParams titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); titleParams.addRule(CENTER_IN_PARENT, TRUE); addView(titleTextView, titleParams);
LayoutParams rightParams = new LayoutParams((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics())); rightParams.addRule(ALIGN_PARENT_RIGHT, TRUE); addView(rightImg, rightParams);
leftImg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (imgClickListener!=null) { imgClickListener.leftImgClick(); } } }); rightImg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (imgClickListener!=null) { imgClickListener.rightImgClick(); } } });
} }
|