TitleHeaderBar.java
7.4 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.xdy.ui.viewgroup;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.xdy.ui.R;
/**
* @author 蒋洪波
* @version 1.0
* @file TitleHeaderBar.java
* @brief 标题栏
* @date 2017/10/13
* Copyright (c) 2017
* All rights reserved.
*/
public class TitleHeaderBar extends RelativeLayout {
private View mDivider;
private TextView mRightTextVeiw;
private TextView mCenterTitleTextView;
private ImageView mLeftReturnImageView;
private ImageView mRightImageView;
private RelativeLayout mLeftViewContainer;
private RelativeLayout mRightViewContainer;
private RelativeLayout mCenterViewContainer;
private String mTitle;
private int mCustomBackGroundColorId = -1;
private boolean enableShowDivider = false;
public TitleHeaderBar(Context context) {
this(context, null);
}
public TitleHeaderBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TitleHeaderBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(this.getHeaderViewLayoutId(), this);
this.mLeftViewContainer = (RelativeLayout) this.findViewById(R.id.rl_title_bar_left);
this.mCenterViewContainer = (RelativeLayout) this.findViewById(R.id.rl_title_bar_center);
this.mRightViewContainer = (RelativeLayout) this.findViewById(R.id.rl_title_bar_right);
this.mLeftReturnImageView = (ImageView) this.findViewById(R.id.iv_title_bar_left);
this.mRightImageView = (ImageView) this.findViewById(R.id.iv_title_bar_right);
this.mCenterTitleTextView = (TextView) this.findViewById(R.id.tv_title_bar_title);
this.mRightTextVeiw = (TextView) this.findViewById(R.id.tv_title_bar_right);
this.mDivider = this.findViewById(R.id.title_header_divider);
initStyle();
}
private void initStyle() {
//设置默认颜色
// if (mCustomBackGroundColorId == -1)
// setBackgroundResource(R.color.titlebar_background_dark_theme);
//设置分割线是否显示
if (enableShowDivider)
mDivider.setVisibility(View.VISIBLE);
else
mDivider.setVisibility(View.GONE);
}
protected int getHeaderViewLayoutId() {
return R.layout.ui_title_header_bar;
}
public ImageView getLeftImageView() {
return this.mLeftReturnImageView;
}
public TextView getTitleTextView() {
return this.mCenterTitleTextView;
}
public void setTitle(String title) {
this.mTitle = title;
this.mCenterTitleTextView.setText(title);
}
public void setTitleColor(int color) {
this.mCenterTitleTextView.setTextColor(color);
}
public String getTitle() {
return this.mTitle;
}
private LayoutParams makeLayoutParams(View view) {
ViewGroup.LayoutParams lpOld = view.getLayoutParams();
LayoutParams lp = null;
if (lpOld == null) {
lp = new LayoutParams(-2, -1);
} else {
lp = new LayoutParams(lpOld.width, lpOld.height);
}
return lp;
}
public void setCustomizedLeftView(View view) {
this.mLeftReturnImageView.setVisibility(View.GONE);
this.getLeftViewContainer().setVisibility(View.VISIBLE);
LayoutParams lp = this.makeLayoutParams(view);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
this.getLeftViewContainer().addView(view, lp);
}
/**
* @param view
* @brief 移除左边客制化的View
*/
public void removeCustomizedView(View view) {
if (view != null && this.mLeftReturnImageView != view) {
this.getLeftViewContainer().removeView(view);
this.mLeftReturnImageView.setVisibility(View.VISIBLE);
}
}
public void setCustomizedLeftView(int layoutId) {
View view = inflate(this.getContext(), layoutId, null);
this.setCustomizedLeftView(view);
}
public void setLeftImageViewResouceId(int resouceId) {
mLeftReturnImageView.setBackgroundResource(resouceId);
}
public void setCustomizedCenterView(View view) {
this.mCenterTitleTextView.setVisibility(View.GONE);
LayoutParams lp = this.makeLayoutParams(view);
lp.addRule(13);
this.getCenterViewContainer().addView(view, lp);
}
public void setCustomizedCenterView(int layoutId) {
View view = inflate(this.getContext(), layoutId, null);
this.setCustomizedCenterView(view);
}
public void setCustomizedRightView(View view) {
LayoutParams lp = this.makeLayoutParams(view);
lp.addRule(15);
lp.addRule(11);
this.getRightViewContainer().addView(view, lp);
}
/**
* @param str
* @brief 设置右边字符串
*/
public void setCustomizedRightString(String str) {
this.mRightTextVeiw.setVisibility(View.VISIBLE);
this.mRightTextVeiw.setText(str);
this.mRightImageView.setVisibility(View.GONE);
}
/**
* @param color
* @brief 设置右边字符串颜色
*/
public void setCustomizedRightStringColor(int color) {
this.mRightTextVeiw.setTextColor(color);
}
/**
* @param resId 图片资源ID
* @brief 通过resID直接设置图片
*/
public void setCustomizedRightView(int resId) {
this.mRightTextVeiw.setVisibility(View.GONE);
this.mRightImageView.setVisibility(View.VISIBLE);
this.mRightImageView.setImageResource(resId);
}
public void removeCustomizedRightView() {
this.mRightTextVeiw.setVisibility(View.GONE);
this.mRightImageView.setVisibility(View.GONE);
}
public RelativeLayout getLeftViewContainer() {
return this.mLeftViewContainer;
}
public RelativeLayout getCenterViewContainer() {
return this.mCenterViewContainer;
}
public RelativeLayout getRightViewContainer() {
return this.mRightViewContainer;
}
public void setLeftOnClickListener(OnClickListener l) {
this.mLeftViewContainer.setOnClickListener(l);
}
public void setCenterOnClickListener(OnClickListener l) {
this.mCenterViewContainer.setOnClickListener(l);
}
public void setRightOnClickListener(OnClickListener l) {
this.mRightViewContainer.setOnClickListener(l);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* @param resId
* @brief 设置自定义背景
*/
public void setCustomBackgroundResource(int resId) {
this.mCustomBackGroundColorId = resId;
setBackgroundResource(resId);
}
/**
* @param b 是否显示分割线
* @brief 是否显示分割线, 参考注册登录页面
*/
public void enableShowDivider(boolean b) {
this.enableShowDivider = b;
if (enableShowDivider) {
mDivider.setVisibility(VISIBLE);
} else {
mDivider.setVisibility(GONE);
}
}
public ImageView getRightImageView() {
return mRightImageView;
}
public TextView getRightTextVeiw() {
return mRightTextVeiw;
}
public View getDivider(){
return mDivider;
}
}