wxr
2023-04-23 098ca04f324624d97399c7c1040c46d2a96ebfaa
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package ezviz.ezopensdkcommon.common;
 
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
public class TitleBar extends FrameLayout {
 
    private Drawable mBackground;
    private Drawable mBackButton;
    private int mTextColor;
    
    private Context mContext;
    private ViewGroup mTitleLayout;
    private TextView mTextView;
    private View mTitleView;
    private LinearLayout mLeftLayout;
    private LinearLayout mRightLayout;
    
    private static final int ID_TITLELAYOUT = 1;
    private static final int ID_TEXTVIEW = 2;
    private static final int ID_LEFTLAYOUT = 3;
    private static final int ID_RIGHTLAYOUT = 4;
    
    public TitleBar(Context context) {
        this(context, null);
    }
 
    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = getContext();
        mTextColor = Color.rgb(51, 51, 51);
        mBackground = Utils.getDrawableFromAssetsFile(mContext, "common_title.9.png");
        
        Drawable normal = Utils.getDrawableFromAssetsFile(mContext, "common_title_back.png");
        Drawable pressed = Utils.getDrawableFromAssetsFile(mContext, "common_title_back_sel.png");
        mBackButton = Utils.newSelector(mContext, normal, pressed, normal, normal);
 
        init();
    }
 
    private void init() {   
        mTitleLayout = new RelativeLayout(mContext);
        mTitleLayout.setId(ID_TITLELAYOUT); 
        mTitleLayout.setBackgroundDrawable(mBackground);
        LayoutParams titleLayoutLp = new LayoutParams(LayoutParams.MATCH_PARENT, Utils.dip2px(mContext, 44));
        this.addView(mTitleLayout, titleLayoutLp);
        
        mLeftLayout = new LinearLayout(mContext);
        mLeftLayout.setId(ID_LEFTLAYOUT); 
        RelativeLayout.LayoutParams leftLayoutLp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftLayoutLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        leftLayoutLp.addRule(RelativeLayout.CENTER_VERTICAL);
        mLeftLayout.setOrientation(LinearLayout.HORIZONTAL);
        mLeftLayout.setGravity(Gravity.CENTER_VERTICAL);
        mTitleLayout.addView(mLeftLayout, leftLayoutLp);
        
        mTextView = new TextView(mContext);
        mTextView.setId(ID_TEXTVIEW); 
        RelativeLayout.LayoutParams textViewLp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textViewLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTextView.setEllipsize(TruncateAt.END);
        mTextView.setMaxWidth(Utils.dip2px(mContext, 200));
        mTextView.setSingleLine(true);
        mTextView.setTextColor(mTextColor);
        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        mTitleLayout.addView(mTextView, textViewLp);
        
        mRightLayout = new LinearLayout(mContext);
        mRightLayout.setId(ID_RIGHTLAYOUT); 
        RelativeLayout.LayoutParams rightLayoutLp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightLayoutLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rightLayoutLp.addRule(RelativeLayout.CENTER_VERTICAL);
        mRightLayout.setOrientation(LinearLayout.HORIZONTAL);
        mRightLayout.setGravity(Gravity.CENTER_VERTICAL);
        mTitleLayout.addView(mRightLayout, rightLayoutLp);
    }
    
    public void setStyle(int textColor, Drawable background, Drawable backButton) {
        mTextColor = textColor;
        mBackground = background;
        if(backButton != null)
            mBackButton = backButton;
        mTextView.setTextColor(mTextColor);
        mTitleLayout.setBackgroundDrawable(mBackground);
    }
    
    /**
     * 设置标题文本
     * 
     * @param resId
     */
    public TextView setTitle(int resId) {
        return setTitle(mContext.getText(resId));
    }
 
    /**
     * 设置标题文本
     * 
     * @param text
     */
    public TextView setTitle(CharSequence text) {
        mTextView.setText(text);
        mTextView.setVisibility((TextUtils.isEmpty(text) || mTitleView != null) ? View.GONE : View.VISIBLE);
        return mTextView;
    }
 
    /**
     * 设置标题控件
     * 
     * @param
     */
    public View setTitle(View view) {
        if (mTitleView != null) {
            mTitleLayout.removeView(mTitleView);
        }
 
        if (view != null) {
            RelativeLayout.LayoutParams layoutParams = null;
            if (view.getLayoutParams() == null) {
                layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
            } else {
                layoutParams = new RelativeLayout.LayoutParams(view.getLayoutParams());
            }
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            mTitleLayout.addView(view, layoutParams);
            mTextView.setVisibility(View.GONE);
        } else {
            mTextView.setVisibility(View.VISIBLE);
        }
        mTitleView = view;
        return view;
    }
 
    /**
     * 设置标题背景颜色
     * 
     * @param
     */
    public void setBackgroundColor(int color) {
        mTitleLayout.setBackgroundColor(color);
    }
 
    /**
     * 标题文本点击事件
     * 
     * @param l
     */
    public void setOnTitleClickListener(OnClickListener l) {
        mTextView.setOnClickListener(l);
    }
 
    /**
     * 在标题文本侧添加一个按钮
     * 
     * @param resId
     * @param l
     * @return
     */
    public ImageView addTitleButton(int resId, OnClickListener l) {
        // return addTitleButton(resId, l);
        ImageView button = new ImageView(mContext);
        button.setImageResource(resId);
        button.setOnClickListener(l);
 
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.RIGHT_OF, ID_TEXTVIEW);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
 
        mTitleLayout.addView(button, layoutParams);
 
        return button;
    }
 
    /**
     * 在标题文本侧添加一个自定义View
     * 
     * @param v
     * @return
     */
    public void addTitleView(View v) {
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.RIGHT_OF, ID_TEXTVIEW);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
 
        mTitleLayout.addView(v, layoutParams);
    }
 
    /**
     * 在标题栏左侧添加一个按钮
     * 
     * @param resId
     * @param l
     * @return
     */
    public Button addLeftButton(int resId, OnClickListener l) {
        return addLeftButton(getResources().getDrawable(resId), l);
    }
 
    /**
     * 在标题栏左侧添加一个按钮
     * 
     * @param drawable
     * @param l
     * @return
     */
    public Button addLeftButton(Drawable drawable, OnClickListener l) {
        Button button = new Button(mContext);
        button.setBackgroundDrawable(drawable);
        button.setOnClickListener(l);
        addLeftView(button);
        return button;
    }
 
    /**
     * 在标题栏左侧添加一个自定义View
     * 
     * @param v
     * @return
     */
    public void addLeftView(View v) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 60), Utils.dip2px(mContext, 44));
        mLeftLayout.addView(v, 0, layoutParams);
    }
 
    /**
     * 清除在标题栏左侧控件
     * 
     * @param v
     * @return
     */
    public void removeLeftView(View v) {
        mLeftLayout.removeView(v);
    }
 
    /**
     * 清除在标题栏左侧控件
     * 
     * @return
     */
    public void removeAllLeftView() {
        mLeftLayout.removeAllViews();
    }
 
    /**
     * 在标题栏左侧添加一个返回按钮
     * 
     * @param l
     * @return
     */
    public Button addBackButton(OnClickListener l) {
        return addLeftButton(mBackButton, l);
    }
 
    /**
     * 在标题栏右侧添加一个按钮
     * 
     * @param resId
     * @param l
     * @return
     */
    public Button addRightButton(int resId, OnClickListener l) {
        return addRightButton(getResources().getDrawable(resId), l);
    }
 
    /**
     * 在标题栏右侧添加一个按钮
     * 
     * @param drawable
     * @param l
     * @return
     */
    public Button addRightButton(Drawable drawable, OnClickListener l) {
        Button button = new Button(mContext);
        button.setBackgroundDrawable(drawable);
        button.setOnClickListener(l);
        addRightView(button);
        return button;
    }
 
    /**
     * 在标题栏右侧添加一个自定义View
     * 
     * @param v
     * @return
     */
    public void addRightView(View v) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 60), Utils.dip2px(mContext, 44));
        mRightLayout.addView(v, 0, layoutParams);
    }
 
    /**
     * 清除在标题栏右侧控件
     * 
     * @param v
     * @return
     */
    public void removeRightView(View v) {
        mRightLayout.removeView(v);
    }
 
    /**
     * 清除在标题栏右侧控件
     * 
     * @return
     */
    public void removeAllRightView() {
        mRightLayout.removeAllViews();
    }
 
    /**
     * 在标题栏右侧添加一个进度条
     * 
     * @return
     */
    public ImageView addRightProgress() {        
        ImageView view = new ImageView(mContext);
        view.setImageBitmap(Utils.getImageFromAssetsFile(mContext, "common_title_refresh.png"));
        addRightView(view);      
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        layoutParams.rightMargin = Utils.dip2px(mContext, 13);
        layoutParams.width = Utils.dip2px(mContext, 30);
        layoutParams.height = Utils.dip2px(mContext, 30);
        
        return view;
    }
 
    /**
     * 在标题栏右侧添加一个按钮
     * 
     * @param text
     * @param l
     * @return
     */
    public Button addRightTextButton(CharSequence text, OnClickListener l) {
        Button button = new Button(mContext);
        Drawable normal = Utils.getDrawableFromAssetsFile(mContext, "tittel_button_bg.9.png");
        Drawable pressed = Utils.getDrawableFromAssetsFile(mContext, "tittel_button_press_bg.9.png");
        button.setBackgroundDrawable(Utils.newSelector(mContext, normal, pressed, normal, normal));
        button.setOnClickListener(l);
        button.setText(text);
        button.setGravity(Gravity.CENTER);
        button.setTextColor(Color.rgb(51, 51, 51));
        button.setPadding(Utils.dip2px(mContext, 5), 0, Utils.dip2px(mContext, 5), 0);
        addRightView(button);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
        layoutParams.rightMargin = Utils.dip2px(mContext, 15);
        return button;
    }
 
    /**
     * 在标题栏右侧添加一个可选择文字
     * 
     * @param text
     * @param checkedText
     * @param l
     * @return
     */
    public CheckTextButton addRightCheckedText(final CharSequence text, final CharSequence checkedText,
            final OnCheckedChangeListener l) {
        CheckTextButton view = new CheckTextButton(mContext);
        int padding = Utils.dip2px(mContext, 5);
        view.setClickable(true);
        view.setPadding(padding, Utils.dip2px(mContext, 9), padding, padding);
        view.setText(text);
        view.setTextColor(Color.rgb(51, 51, 51));
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        view.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                buttonView.setText(isChecked ? checkedText : text);
                if (l != null)
                    l.onCheckedChanged(buttonView, isChecked);
            }
        });
 
        addRightView(view);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        layoutParams.rightMargin = Utils.dip2px(mContext, 15) - padding;
        return view;
    }
 
    public void setBackButton(int resId) {
        mBackButton = getResources().getDrawable(resId);
    }
}