1 import android.content.Context;
2 import android.content.res.TypedArray;
3 import android.graphics.drawable.Drawable;
4 import android.util.AttributeSet;
5 import android.view.LayoutInflater;
6 import android.widget.ImageView;
7 import android.widget.RelativeLayout;
8 import android.widget.TextView;
9
10 import androidx.annotation.ColorInt;
11 import androidx.annotation.DrawableRes;
12 import androidx.annotation.StringRes;
13
14 import com.vocational.base.R;
15
16 /**
17 * 自定义的标题栏
18 *
19 * @author Silence
20 * @since 1.0
21 */
22 public class CustomTitleView extends RelativeLayout {
23
24 private ImageView leftImage;
25 private TextView leftTitle;
26 private TextView title;
27 private TextView rightTitle;
28 private ImageView rightImage;
29
30 public CustomTitleView(Context context) {
31 super(context);
32 }
33
34 public CustomTitleView(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 init(context, attrs);
37 }
38
39 public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
40 super(context, attrs, defStyleAttr);
41 init(context, attrs);
42 }
43
44 private void init(Context context, AttributeSet attrs) {
45 // 加载布局
46 LayoutInflater.from(context).inflate(R.layout.custom_title_view_layout, this, true);
47
48 // 初始化控件
49 leftImage = findViewById(R.id.custom_title_view_left_image);
50 leftTitle = findViewById(R.id.custom_title_view_left_title);
51 title = findViewById(R.id.custom_title_view_title);
52 rightTitle = findViewById(R.id.custom_title_view_right_title);
53 rightImage = findViewById(R.id.custom_title_view_right_image);
54
55 // 获取资源并设置
56 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView);
57
58 // 左侧图片
59 Drawable leftSrc = typedArray.getDrawable(R.styleable.CustomTitleView_custom_title_view_left_src);
60 // 左侧是否显示
61 boolean leftVisibility = typedArray.getBoolean(R.styleable.CustomTitleView_custom_title_view_left_visibility, true);
62 setLeftImageSrc(leftSrc);
63 setLeftImageVisibility(leftVisibility);
64
65 String leftTitleStr = typedArray.getString(R.styleable.CustomTitleView_custom_title_view_left_title);
66 float leftTitleSize = typedArray.getDimension(R.styleable.CustomTitleView_custom_title_view_left_title_text_size, 12f);
67 int leftTitleColor = typedArray.getColor(R.styleable.CustomTitleView_custom_title_view_left_title_text_color, 0x000000);
68 setLeftTitleStr(leftTitleStr);
69 setLeftTitleSize(leftTitleSize);
70 setLeftTitleColor(leftTitleColor);
71
72 String titleStr = typedArray.getString(R.styleable.CustomTitleView_custom_title_view_title);
73 float titleSize = typedArray.getDimension(R.styleable.CustomTitleView_custom_title_view_title_text_size, 12f);
74 int titleColor = typedArray.getColor(R.styleable.CustomTitleView_custom_title_view_title_text_color, 0x000000);
75 setTitleStr(titleStr);
76 setTitleSize(titleSize);
77 setTitleColor(titleColor);
78
79 String rightTitleStr = typedArray.getString(R.styleable.CustomTitleView_custom_title_view_right_title);
80 float rightTitleSize = typedArray.getDimension(R.styleable.CustomTitleView_custom_title_view_right_title_text_size, 12f);
81 int rightTitleColor = typedArray.getColor(R.styleable.CustomTitleView_custom_title_view_right_title_text_color, 0x000000);
82 setRightTitleStr(rightTitleStr);
83 setRightTitleSize(rightTitleSize);
84 setRightTitleColor(rightTitleColor);
85
86 // 右侧图片
87 Drawable rightSrc = typedArray.getDrawable(R.styleable.CustomTitleView_custom_title_view_right_src);
88 // 右侧是否显示
89 boolean rightVisibility = typedArray.getBoolean(R.styleable.CustomTitleView_custom_title_view_right_visibility, false);
90 setRightImageSrc(rightSrc);
91 setRightImageVisibility(rightVisibility);
92
93 // 释放资源
94 typedArray.recycle();
95 }
96
97 public void setLeftImageSrc(@DrawableRes int imageSrc) {
98 if (leftImage != null) {
99 leftImage.setImageResource(imageSrc);
100 }
101 }
102
103 public void setLeftImageSrc(Drawable imageSrc) {
104 if (leftImage != null) {
105 leftImage.setImageDrawable(imageSrc);
106 }
107 }
108
109 public void setLeftImageVisibility(boolean visibility) {
110 if (leftImage != null) {
111 leftImage.setVisibility(visibility ? VISIBLE : GONE);
112 }
113 }
114
115 public void setLeftTitleStr(CharSequence str) {
116 if (leftTitle != null) {
117 leftTitle.setText(str);
118 }
119 }
120
121 public void setLeftTitleStr(@StringRes int str) {
122 if (leftTitle != null) {
123 leftTitle.setText(str);
124 }
125 }
126
127 public void setLeftTitleSize(float size) {
128 if (leftTitle != null) {
129 leftTitle.getPaint().setTextSize(size);
130 }
131 }
132
133 public void setLeftTitleColor(@ColorInt int color) {
134 if (leftTitle != null) {
135 leftTitle.setTextColor(color);
136 }
137 }
138
139 public void setTitleStr(CharSequence str) {
140 if (title != null) {
141 title.setText(str);
142 }
143 }
144
145 public void setTitleStr(@StringRes int str) {
146 if (title != null) {
147 title.setText(str);
148 }
149 }
150
151 public void setTitleSize(float size) {
152 if (title != null) {
153 title.getPaint().setTextSize(size);
154 }
155 }
156
157 public void setTitleColor(@ColorInt int color) {
158 if (title != null) {
159 title.setTextColor(color);
160 }
161 }
162
163 public void setRightTitleStr(CharSequence str) {
164 if (rightTitle != null) {
165 rightTitle.setText(str);
166 }
167 }
168
169 public void setRightTitleStr(@StringRes int str) {
170 if (rightTitle != null) {
171 rightTitle.setText(str);
172 }
173 }
174
175 public void setRightTitleSize(float size) {
176 if (rightTitle != null) {
177 rightTitle.getPaint().setTextSize(size);
178 }
179 }
180
181 public void setRightTitleColor(@ColorInt int color) {
182 if (rightTitle != null) {
183 rightTitle.setTextColor(color);
184 }
185 }
186
187 public void setRightImageSrc(@DrawableRes int imageSrc) {
188 if (rightImage != null) {
189 rightImage.setImageResource(imageSrc);
190 }
191 }
192
193 public void setRightImageSrc(Drawable imageSrc) {
194 if (rightImage != null) {
195 rightImage.setImageDrawable(imageSrc);
196 }
197 }
198
199 public void setRightImageVisibility(boolean visibility) {
200 if (rightImage != null) {
201 rightImage.setVisibility(visibility ? VISIBLE : GONE);
202 }
203 }
204 }