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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| 1.第一步:定义的类要继承ViewGroup然后监听点击事件(因为自定义titlebar常常为其内到菜单事件作点击交互) 2.第二步:定义显示大小参数以及控件变量名 3.第三步: 定义参数方法以便后期调用 private static final int DEFAULT_MAIN_TEXT_SIZE = 18; private static final int DEFAULT_SUB_TEXT_SIZE = 12; private static final int DEFAULT_ACTION_TEXT_SIZE = 15; private static final int DEFAULT_TITLE_BAR_HEIGHT = 48;
private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";
private TextView mLeftText; private LinearLayout mRightLayout; private LinearLayout mCenterLayout; private TextView mCenterText; private TextView mSubTitleText; private View mCustomCenterView; private View mDividerView;
private boolean mImmersive;
private int mScreenWidth; private int mStatusBarHeight; private int mActionPadding; private int mOutPadding; private int mActionTextColor; private int mHeight;
public TitleBar(Context context) { super(context); init(context); }
public TitleBar(Context context, AttributeSet attrs) { super(context, attrs); init(context); }
public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); }
private void init(Context context) { if (mImmersive) { mStatusBarHeight = getStatusBarHeight(); } mActionPadding = dip2px(5); mOutPadding = dip2px(8); mHeight = dip2px(DEFAULT_TITLE_BAR_HEIGHT); initView(context); }
private void initView(Context context) { mLeftText = new TextView(context); mCenterLayout = new LinearLayout(context); mRightLayout = new LinearLayout(context); mDividerView = new View(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
mLeftText.setTextSize(DEFAULT_ACTION_TEXT_SIZE); mLeftText.setSingleLine(); mLeftText.setGravity(Gravity.CENTER_VERTICAL); mLeftText.setPadding(mOutPadding + mActionPadding, 0, mOutPadding, 0);
mCenterText = new TextView(context); mSubTitleText = new TextView(context); mCenterLayout.addView(mCenterText); mCenterLayout.addView(mSubTitleText);
mCenterLayout.setGravity(Gravity.CENTER); mCenterText.setTextSize(DEFAULT_MAIN_TEXT_SIZE); mCenterText.setSingleLine(); mCenterText.setGravity(Gravity.CENTER); mCenterText.setEllipsize(TextUtils.TruncateAt.END);
mSubTitleText.setTextSize(DEFAULT_SUB_TEXT_SIZE); mSubTitleText.setSingleLine(); mSubTitleText.setGravity(Gravity.CENTER); mSubTitleText.setEllipsize(TextUtils.TruncateAt.END);
mRightLayout.setPadding(mOutPadding, 0, mOutPadding, 0);
addView(mLeftText, layoutParams); addView(mCenterLayout); addView(mRightLayout, layoutParams); addView(mDividerView, new LayoutParams(LayoutParams.MATCH_PARENT, 1)); } public void setDividerColor(int color) { mDividerView.setBackgroundColor(color); } @Override public void onClick(View view) { final Object tag = view.getTag(); if (tag instanceof Action) { final Action action = (Action) tag; action.performAction(view); } } public View addAction(Action action) { final int index = mRightLayout.getChildCount(); return addAction(action, index); }
/** * Adds a new {@link Action} at the specified index. * @param action the action to add * @param index the position at which to add the action */ public View addAction(Action action, int index) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); View view = inflateAction(action); mRightLayout.addView(view, index, params); return view; } public void setTitle(int resid) { setTitle(getResources().getString(resid)); }
public void setTitleColor(int resid) { mCenterText.setTextColor(resid); } public void setSubTitleColor(int resid) { mSubTitleText.setTextColor(resid); } private void setTitle(CharSequence title, CharSequence subTitle, int orientation) { mCenterLayout.setOrientation(orientation); mCenterText.setText(title);
mSubTitleText.setText(subTitle); mSubTitleText.setVisibility(View.VISIBLE); } public void setHeight(int height) { mHeight = height; setMeasuredDimension(getMeasuredWidth(), mHeight); }
|