-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPubTitleView.java
242 lines (200 loc) · 8.01 KB
/
PubTitleView.java
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
package com.sum.library.view.widget;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sum.library.R;
/**
* Created by sdl on 2019-06-19.
*/
public class PubTitleView extends LinearLayout {
private Context context;
public PubTitleView(Context context) {
this(context, null, 0);
}
public PubTitleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PubTitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PubTitleView);
int layoutId = array.getResourceId(R.styleable.PubTitleView_title_view, R.layout.pub_title_view);
initLayoutView(context, layoutId);
//内容
mTitleTextView.setText(array.getString(R.styleable.PubTitleView_title_default_name));
//字体大小
int defaultSize = context.getResources().getDimensionPixelSize(R.dimen.pub_title_text_size);
int size = array.getDimensionPixelSize(R.styleable.PubTitleView_title_default_name_text_size, defaultSize);
mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
//返回键
int resourceId = array.getResourceId(R.styleable.PubTitleView_title_default_back_img, -1);
if (resourceId != -1) {
mTitleBackImage.setImageResource(resourceId);
}
//返回键展示
boolean backShow = array.getBoolean(R.styleable.PubTitleView_title_default_back_img_show, true);
if (backShow) {
mTitleBackImage.setVisibility(View.VISIBLE);
} else {
mTitleBackImage.setVisibility(View.GONE);
}
//背景色
int bgColor = array.getColor(R.styleable.PubTitleView_title_default_bg_color, -1);
//优先设置资源
int bgRes = array.getResourceId(R.styleable.PubTitleView_title_default_bg_resource, -1);
if (bgRes != -1) {
setBackgroundResource(bgRes);
} else if (bgColor != -1) {
setBackgroundColor(bgColor);
}
//状态栏适配
if (array.getBoolean(R.styleable.PubTitleView_title_default_adjust_bar, false)) {
int barColor = array.getColor(R.styleable.PubTitleView_title_default_adjust_bar_color, -1);
if (barColor != -1) {
addStatusBarHeight(barColor);
} else {
addStatusBarHeight();
}
}
//主题色
boolean white = array.getBoolean(R.styleable.PubTitleView_title_default_white_theme, false);
if (white) {
Drawable drawable = mTitleBackImage.getDrawable();
if (drawable != null) {
DrawableCompat.setTint(drawable, Color.WHITE);
}
mTitleTextView.setTextColor(Color.WHITE);
} else {
//字体颜色
int titleColor = array.getColor(R.styleable.PubTitleView_title_default_name_text_color, ContextCompat.getColor(context, R.color.pub_title_text_color));
mTitleTextView.setTextColor(titleColor);
}
//返回键tint
int backImageTint = array.getColor(R.styleable.PubTitleView_title_default_back_img_tint, -1);
if (backImageTint != -1) {
Drawable drawable = mTitleBackImage.getDrawable();
if (drawable != null) {
DrawableCompat.setTint(drawable, backImageTint);
}
}
array.recycle();
}
private View mView;
private TextView mTitleTextView = null;//标题
private ImageView mTitleBackImage = null;//返回
private LinearLayout mTitleRightContent = null;//右侧按钮
public ImageView getTitleBackImage() {
return mTitleBackImage;
}
public TextView getTitleTextView() {
return mTitleTextView;
}
private void initLayoutView(Context context, int layoutId) {
setOrientation(LinearLayout.VERTICAL);
View view = LayoutInflater.from(context).inflate(layoutId, this, true);
initView(view);
mView = view;
}
private void initView(View view) {
mTitleTextView = view.findViewById(R.id.pub_title_text);
mTitleBackImage = view.findViewById(R.id.pub_title_back);
mTitleRightContent = view.findViewById(R.id.pub_title_right);
mTitleBackImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (getContext() instanceof Activity) {
((Activity) getContext()).onBackPressed();
}
}
});
}
//插入一个状态栏高度的view
private void addStatusBarHeight() {
View space = new View(context);
addView(space, 0, new LayoutParams(-1, getStatusBarHeight()));
}
private void addStatusBarHeight(int color) {
View space = new View(context);
space.setBackgroundColor(color);
addView(space, 0, new LayoutParams(-1, getStatusBarHeight()));
}
private int dp2px(Float dpValue) {
float scale = Resources.getSystem().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
private int getStatusBarHeight() {
Resources resources = Resources.getSystem();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}
//设置标题
public void setTitle(CharSequence title) {
if (mTitleTextView != null) {
mTitleTextView.setText(title);
}
}
//添加文本按钮
public TextView addRightTextButton(String btnName, OnClickListener clickListener) {
return addRightTextButton(btnName, R.color.pub_title_text_right_color, clickListener);
}
//添加文本按钮
public TextView addRightTextButton(String btnName, int colorRes, OnClickListener clickListener) {
TextView view = new TextView(context);
view.setText(btnName);
view.setTextSize(14f);
view.setGravity(Gravity.CENTER);
view.setPadding(0, 0, dp2px(12f), 0);
view.setTextColor(ContextCompat.getColor(context, colorRes));
view.setOnClickListener(clickListener);
addRightView(view);
return view;
}
//添加图片按钮
public ImageView addRightImageButton(int imageSrc, OnClickListener clickListener) {
ImageView view = new ImageView(context);
view.setImageResource(imageSrc);
view.setScaleType(ImageView.ScaleType.CENTER);
view.setPadding(0, 0, dp2px(12f), 0);
view.setOnClickListener(clickListener);
addRightView(view);
return view;
}
//添加按钮
public View addRightView(View view) {
mTitleRightContent.addView(view, 0, new ViewGroup.LayoutParams(-2, -1));
return view;
}
//添加按钮
public View addRightView(View view, int width, int height) {
mTitleRightContent.addView(view, 0, new ViewGroup.LayoutParams(width, height));
return view;
}
//添加按钮
public View addRightView(View view, ViewGroup.LayoutParams params) {
mTitleRightContent.addView(view, 0, params);
return view;
}
//添加按钮
public View addRightCustomView(View view) {
mTitleRightContent.addView(view);
return view;
}
public LinearLayout getTitleRightContent() {
return mTitleRightContent;
}
}