Skip to content

Commit 6080f1f

Browse files
优化TitleBarView
1 parent 79a18f4 commit 6080f1f

File tree

5 files changed

+96
-170
lines changed

5 files changed

+96
-170
lines changed

app/src/main/java/com/pengxh/androidx/lib/view/MainActivity.java

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
package com.pengxh.androidx.lib.view;
22

3-
import android.content.pm.ApplicationInfo;
4-
import android.content.pm.PackageInfo;
5-
import android.content.pm.PackageManager;
63
import android.os.Bundle;
7-
import android.util.Log;
84

95
import androidx.annotation.Nullable;
106

117
import com.pengxh.androidx.lib.databinding.ActivityMainBinding;
128
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
13-
import com.pengxh.androidx.lite.hub.ObjectHub;
149
import com.pengxh.androidx.lite.hub.StringHub;
15-
16-
import java.util.ArrayList;
17-
import java.util.Collections;
18-
import java.util.Comparator;
19-
import java.util.List;
20-
import java.util.Locale;
10+
import com.pengxh.androidx.lite.widget.TitleBarView;
2111

2212
public class MainActivity extends AndroidxBaseActivity<ActivityMainBinding> {
2313

@@ -35,68 +25,17 @@ protected void initOnCreate(@Nullable Bundle savedInstanceState) {
3525

3626
@Override
3727
protected void initEvent() {
38-
ArrayList<String> list = new ArrayList<>();
39-
PackageManager packageManager = getPackageManager();
40-
List<PackageInfo> packages = packageManager.getInstalledPackages(0);
41-
for (int i = 0; i < packages.size(); i++) {
42-
PackageInfo packageInfo = packages.get(i);
43-
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
44-
//非系统应用
45-
String appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
46-
list.add(appName.replace(" ", "").replace("-", "")
47-
.replace("(", "").replace(")", ""));
48-
}
49-
}
50-
list.add("Play商店");
51-
list.add("设置");
52-
list.add("相机");
53-
list.add("相册");
54-
list.add("浏览器");
55-
56-
ArrayList<App> apps = new ArrayList<>();
57-
for (String s : list) {
58-
App app = new App();
59-
app.setName(s);
60-
if (s.contains("7881")) {
61-
app.setPy("QIBABAYIYOUXIJIAOYI");
62-
} else if (s.contains("长沙")) {
63-
app.setPy("CHANGSHADITIE");
64-
} else if (s.contains("铁路12306")) {
65-
app.setPy("TIELUYIERSANLINGLIU");
66-
} else {
67-
app.setPy(StringHub.getHanYuPinyin(s).toUpperCase(Locale.ROOT));
28+
binding.titleView.setOnClickListener(new TitleBarView.OnClickListener() {
29+
@Override
30+
public void onLeftClick() {
31+
StringHub.show(MainActivity.this, "onLeftClick");
6832
}
69-
apps.add(app);
70-
}
7133

72-
Collections.sort(apps, new Comparator<App>() {
7334
@Override
74-
public int compare(App o1, App o2) {
75-
return o1.py.compareTo(o2.py);
35+
public void onRightClick() {
36+
StringHub.show(MainActivity.this, "onRightClick");
7637
}
7738
});
78-
Log.d(TAG, ObjectHub.getJson(apps));
79-
}
80-
81-
static class App {
82-
private String name;
83-
private String py;
84-
85-
public String getName() {
86-
return name;
87-
}
88-
89-
public void setName(String name) {
90-
this.name = name;
91-
}
92-
93-
public String getPy() {
94-
return py;
95-
}
96-
97-
public void setPy(String py) {
98-
this.py = py;
99-
}
10039
}
10140

10241
@Override
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:gravity="center"
76
android:orientation="vertical">
87

8+
<com.pengxh.androidx.lite.widget.TitleBarView
9+
android:id="@+id/titleView"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:background="@color/purple_500"
13+
app:tbv_left_image="@drawable/ic_title_left"
14+
app:tbv_right_image="@drawable/ic_title_right"
15+
app:tbv_show_left_image="true"
16+
app:tbv_show_right_image="true"
17+
app:tbv_smaller_title="false"
18+
app:tbv_text="@string/app_name"
19+
app:tbv_text_color="@color/white" />
20+
921
<com.pengxh.androidx.lite.widget.AirDashBoardView
1022
android:id="@+id/airDashBoardView"
1123
android:layout_width="wrap_content"
1224
android:layout_height="wrap_content"
25+
android:layout_centerInParent="true"
1326
app:air_center_text_color="@color/red"
1427
app:air_center_text_size="@dimen/sp_18"
1528
app:air_ring_background="@color/mainBackground"
1629
app:air_ring_foreground="@color/purple_500"
1730
app:air_ring_radius="@dimen/dp_100"
1831
app:air_ring_stroke="@dimen/dp_15" />
19-
</LinearLayout>
32+
</RelativeLayout>

lite/src/main/java/com/pengxh/androidx/lite/widget/TitleBarView.java

Lines changed: 40 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,123 +4,66 @@
44
import android.content.res.TypedArray;
55
import android.graphics.Canvas;
66
import android.graphics.Color;
7-
import android.text.TextUtils;
87
import android.util.AttributeSet;
9-
import android.view.Gravity;
10-
import android.view.View;
11-
import android.widget.ImageView;
12-
import android.widget.RelativeLayout;
13-
import android.widget.TextView;
8+
import android.view.LayoutInflater;
9+
import android.widget.LinearLayout;
1410

1511
import com.pengxh.androidx.lite.R;
16-
import com.pengxh.androidx.lite.hub.ContextHub;
17-
import com.pengxh.androidx.lite.hub.IntHub;
12+
import com.pengxh.androidx.lite.databinding.WidgetViewTitleBarBinding;
1813

1914
/**
2015
* 界面顶部标题栏
2116
*/
22-
public class TitleBarView extends RelativeLayout {
17+
public class TitleBarView extends LinearLayout {
2318

24-
private final int titleHeight = IntHub.dp2px(getContext(), 45);
25-
private final TextView textView;
19+
private final WidgetViewTitleBarBinding binding;
2620

2721
public TitleBarView(Context context, AttributeSet attrs) {
2822
super(context, attrs);
23+
binding = WidgetViewTitleBarBinding.inflate(LayoutInflater.from(context), this, true);
24+
2925
TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
3026
int leftImageRes = type.getResourceId(R.styleable.TitleBarView_tbv_left_image, R.drawable.ic_title_left);
3127
boolean isShowLeft = type.getBoolean(R.styleable.TitleBarView_tbv_show_left_image, true);
3228
int rightImageRes = type.getResourceId(R.styleable.TitleBarView_tbv_right_image, R.drawable.ic_title_right);
3329
boolean isShowRight = type.getBoolean(R.styleable.TitleBarView_tbv_show_right_image, false);
3430
CharSequence title = type.getText(R.styleable.TitleBarView_tbv_text);
3531
int titleColor = type.getColor(R.styleable.TitleBarView_tbv_text_color, Color.WHITE);
36-
float titleSize = type.getDimension(R.styleable.TitleBarView_tbv_text_size, 18f);
37-
boolean onlyShowTitle = type.getBoolean(R.styleable.TitleBarView_tbv_only_show_title, false);
32+
boolean isSmallerTitle = type.getBoolean(R.styleable.TitleBarView_tbv_smaller_title, false);
3833
type.recycle();
3934

40-
if (onlyShowTitle) {
41-
//文字
42-
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
43-
titleParams.height = titleHeight;
44-
textView = new TextView(context);
45-
textView.setText(title);
46-
textView.setSingleLine(true);
47-
textView.setEllipsize(TextUtils.TruncateAt.END);
48-
//textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
49-
textView.setTextSize(titleSize / ContextHub.getScreenDensity(context));
50-
textView.setGravity(Gravity.CENTER);
51-
textView.setTextColor(titleColor);
52-
titleParams.addRule(CENTER_IN_PARENT, TRUE);
53-
textView.setLayoutParams(titleParams);
54-
addView(textView);
55-
} else {
56-
int iconSize = IntHub.dp2px(getContext(), 25);
57-
int textMargin = IntHub.dp2px(getContext(), 10);
58-
59-
//左边图标
60-
if (isShowLeft) {
61-
RelativeLayout.LayoutParams leftImageParams = new RelativeLayout.LayoutParams(iconSize, iconSize);
62-
ImageView leftImageView = new ImageView(context);
63-
leftImageView.setImageResource(leftImageRes);
64-
leftImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
65-
leftImageParams.setMarginStart(textMargin);
66-
leftImageParams.addRule(CENTER_VERTICAL, TRUE);
67-
addView(leftImageView, leftImageParams);
68-
leftImageView.setOnClickListener(new View.OnClickListener() {
69-
@Override
70-
public void onClick(View v) {
71-
try {
72-
listener.onLeftClick();
73-
} catch (NullPointerException e) {
74-
e.printStackTrace();
75-
}
76-
}
77-
});
78-
}
79-
80-
//文字
81-
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
82-
titleParams.height = titleHeight;
83-
textView = new TextView(context);
84-
textView.setText(title);
85-
textView.setSingleLine(true);
86-
textView.setEllipsize(TextUtils.TruncateAt.END);
87-
//textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
88-
textView.setTextSize(titleSize / ContextHub.getScreenDensity(context));
89-
textView.setGravity(Gravity.CENTER);
90-
textView.setTextColor(titleColor);
91-
titleParams.addRule(CENTER_IN_PARENT, TRUE);
92-
textView.setLayoutParams(titleParams);
93-
addView(textView);
94-
95-
//右边图标
96-
if (isShowRight) {
97-
RelativeLayout.LayoutParams rightImageParams = new RelativeLayout.LayoutParams(iconSize, iconSize);
98-
ImageView rightImageView = new ImageView(context);
99-
rightImageView.setImageResource(rightImageRes);
100-
rightImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
101-
rightImageParams.setMarginEnd(textMargin);
102-
rightImageParams.addRule(CENTER_VERTICAL, TRUE);
103-
rightImageParams.addRule(ALIGN_PARENT_END, TRUE);
104-
addView(rightImageView, rightImageParams);
105-
rightImageView.setOnClickListener(new View.OnClickListener() {
106-
@Override
107-
public void onClick(View v) {
108-
try {
109-
listener.onRightClick();
110-
} catch (NullPointerException e) {
111-
e.printStackTrace();
112-
}
113-
}
114-
});
115-
}
35+
//左边图标
36+
if (isShowLeft) {
37+
binding.leftButton.setImageResource(leftImageRes);
38+
binding.leftButton.setOnClickListener(v -> {
39+
if (listener == null) {
40+
throw new NullPointerException("listener is null");
41+
}
42+
listener.onLeftClick();
43+
});
11644
}
117-
}
11845

119-
@Override
120-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
121-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
122-
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
123-
setMeasuredDimension(widthSpecSize, titleHeight);
46+
//文字
47+
binding.titleView.setText(title);
48+
float textSize;
49+
if (isSmallerTitle) {
50+
textSize = 16f;
51+
} else {
52+
textSize = 18f;
53+
}
54+
binding.titleView.setTextSize(textSize);
55+
binding.titleView.setTextColor(titleColor);
56+
57+
//右边图标
58+
if (isShowRight) {
59+
binding.rightButton.setImageResource(rightImageRes);
60+
binding.rightButton.setOnClickListener(v -> {
61+
if (listener == null) {
62+
throw new NullPointerException("listener is null");
63+
}
64+
listener.onRightClick();
65+
});
66+
}
12467
}
12568

12669
@Override
@@ -134,15 +77,15 @@ protected void onDraw(Canvas canvas) {
13477
* 动态设置标题
13578
*/
13679
public void setTitle(String title) {
137-
textView.setText(title);
80+
binding.titleView.setText(title);
13881
invalidate();
13982
}
14083

14184
/**
14285
* 获取当前显示标题文字
14386
*/
14487
public String getTitle() {
145-
return textView.getText().toString();
88+
return binding.titleView.getText().toString();
14689
}
14790

14891
private OnClickListener listener;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/rootView"
4+
android:layout_width="match_parent"
5+
android:layout_height="@dimen/lib_dp_45"
6+
android:orientation="horizontal">
7+
8+
<ImageView
9+
android:id="@+id/leftButton"
10+
android:layout_width="@dimen/lib_dp_25"
11+
android:layout_height="@dimen/lib_dp_25"
12+
android:layout_gravity="center_vertical"
13+
android:layout_margin="@dimen/lib_dp_10"
14+
android:scaleType="centerInside" />
15+
16+
<TextView
17+
android:id="@+id/titleView"
18+
android:layout_width="0dp"
19+
android:layout_height="match_parent"
20+
android:layout_marginVertical="@dimen/lib_dp_10"
21+
android:layout_weight="1"
22+
android:gravity="center"
23+
android:singleLine="true" />
24+
25+
<ImageView
26+
android:id="@+id/rightButton"
27+
android:layout_width="@dimen/lib_dp_25"
28+
android:layout_height="@dimen/lib_dp_25"
29+
android:layout_gravity="center_vertical"
30+
android:layout_margin="@dimen/lib_dp_10"
31+
android:scaleType="centerInside" />
32+
</LinearLayout>

lite/src/main/res/values/attrs.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
<!--标题栏中间文字属性-->
4545
<attr name="tbv_text" format="string" />
46-
<attr name="tbv_text_size" format="dimension" />
4746
<attr name="tbv_text_color" format="color" />
48-
<attr name="tbv_only_show_title" format="boolean" />
47+
<attr name="tbv_smaller_title" format="boolean" />
4948
</declare-styleable>
5049
</resources>

0 commit comments

Comments
 (0)