Skip to content

Commit a33d1f6

Browse files
完成TitleBarView优化
1 parent ec77f40 commit a33d1f6

File tree

5 files changed

+77
-98
lines changed

5 files changed

+77
-98
lines changed

app/src/main/res/layout/activity_main.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
android:layout_height="match_parent"
77
android:orientation="vertical">
88

9-
<TextView
9+
<com.pengxh.kt.lite.widget.TitleBarView
10+
android:id="@+id/titleView"
1011
android:layout_width="match_parent"
11-
android:layout_height="@dimen/titleViewHeight"
12+
android:layout_height="wrap_content"
1213
android:background="@color/mainColor"
13-
android:gravity="center"
14-
android:text="Kotlin-lite-lib用法示例"
15-
android:textColor="@color/white"
16-
android:textSize="@dimen/sp_18" />
14+
app:tbv_left_image="@drawable/ic_title_left"
15+
app:tbv_right_image="@drawable/ic_title_right"
16+
app:tbv_show_left_image="false"
17+
app:tbv_show_right_image="false"
18+
app:tbv_smaller_title="false"
19+
app:tbv_text="Kotlin-lite-lib用法示例"
20+
app:tbv_text_color="@color/white" />
1721

1822
<com.google.android.material.tabs.TabLayout
1923
android:id="@+id/tabLayout"

app/src/main/res/layout/fragment_widget_title_bar_view.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
android:layout_height="wrap_content"
1313
android:background="@color/mainColor"
1414
app:tbv_left_image="@drawable/ic_title_left"
15-
app:tbv_only_show_title="false"
1615
app:tbv_right_image="@drawable/ic_title_right"
1716
app:tbv_show_left_image="true"
1817
app:tbv_show_right_image="true"
18+
app:tbv_smaller_title="false"
1919
app:tbv_text="@string/app_name"
20-
app:tbv_text_color="@color/white"
21-
app:tbv_text_size="@dimen/sp_18" />
20+
app:tbv_text_color="@color/white" />
2221
</LinearLayout>

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

Lines changed: 32 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@ package com.pengxh.kt.lite.widget
33
import android.content.Context
44
import android.graphics.Canvas
55
import android.graphics.Color
6-
import android.text.TextUtils
76
import android.util.AttributeSet
8-
import android.view.Gravity
9-
import android.widget.ImageView
10-
import android.widget.RelativeLayout
11-
import android.widget.TextView
7+
import android.view.LayoutInflater
8+
import android.widget.LinearLayout
129
import com.pengxh.kt.lite.R
13-
import com.pengxh.kt.lite.extensions.dp2px
14-
import com.pengxh.kt.lite.extensions.getScreenDensity
10+
import com.pengxh.kt.lite.databinding.WidgetViewTitleBarBinding
1511

1612

1713
/**
1814
* 界面顶部标题栏
1915
* */
20-
class TitleBarView constructor(context: Context, attrs: AttributeSet) :
21-
RelativeLayout(context, attrs) {
16+
class TitleBarView constructor(context: Context, attrs: AttributeSet? = null) :
17+
LinearLayout(context, attrs) {
2218

23-
private val titleHeight = 45.dp2px(context)
24-
private var textView: TextView
19+
private val kTag = "TitleBarView"
20+
private var binding: WidgetViewTitleBarBinding
2521

2622
init {
23+
binding = WidgetViewTitleBarBinding.inflate(LayoutInflater.from(context), this, true)
24+
2725
val type = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView)
2826
val leftImageRes = type.getResourceId(
2927
R.styleable.TitleBarView_tbv_left_image, R.drawable.ic_title_left
@@ -35,86 +33,33 @@ class TitleBarView constructor(context: Context, attrs: AttributeSet) :
3533
val isShowRight = type.getBoolean(R.styleable.TitleBarView_tbv_show_right_image, false)
3634
val title = type.getText(R.styleable.TitleBarView_tbv_text)
3735
val titleColor = type.getColor(R.styleable.TitleBarView_tbv_text_color, Color.WHITE)
38-
val titleSize = type.getDimension(R.styleable.TitleBarView_tbv_text_size, 18f)
39-
val onlyShowTitle = type.getBoolean(R.styleable.TitleBarView_tbv_only_show_title, false)
36+
val isSmallerTitle = type.getBoolean(R.styleable.TitleBarView_tbv_smaller_title, false)
4037
type.recycle()
4138

42-
if (onlyShowTitle) {
43-
//文字
44-
val titleParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
45-
titleParams.height = titleHeight
46-
textView = TextView(context)
47-
textView.text = title
48-
textView.isSingleLine = true
49-
textView.ellipsize = TextUtils.TruncateAt.END
50-
//textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
51-
textView.textSize = titleSize / context.getScreenDensity()
52-
textView.gravity = Gravity.CENTER
53-
textView.setTextColor(titleColor)
54-
titleParams.addRule(CENTER_IN_PARENT, TRUE)
55-
textView.layoutParams = titleParams
56-
addView(textView)
57-
} else {
58-
val iconSize = 25.dp2px(context)
59-
val textMargin = 10.dp2px(context)
60-
61-
//左边图标
62-
if (isShowLeft) {
63-
val leftImageParams = LayoutParams(iconSize, iconSize)
64-
val leftImageView = ImageView(context)
65-
leftImageView.setImageResource(leftImageRes)
66-
leftImageView.scaleType = ImageView.ScaleType.CENTER_INSIDE
67-
leftImageParams.leftMargin = textMargin
68-
leftImageParams.addRule(CENTER_VERTICAL, TRUE)
69-
addView(leftImageView, leftImageParams)
70-
leftImageView.setOnClickListener {
71-
listener?.onLeftClick()
72-
}
39+
//左边图标
40+
if (isShowLeft) {
41+
binding.leftButton.setImageResource(leftImageRes)
42+
binding.leftButton.setOnClickListener {
43+
listener?.onLeftClick()
7344
}
45+
}
7446

75-
//文字
76-
val titleParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
77-
titleParams.height = titleHeight
78-
textView = TextView(context)
79-
textView.text = title
80-
textView.isSingleLine = true
81-
textView.ellipsize = TextUtils.TruncateAt.END
82-
//textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
83-
textView.textSize = titleSize / context.getScreenDensity()
84-
textView.gravity = Gravity.CENTER
85-
textView.setTextColor(titleColor)
86-
titleParams.leftMargin = textMargin
87-
titleParams.rightMargin = textMargin
88-
titleParams.addRule(CENTER_IN_PARENT, TRUE)
89-
titleParams.addRule(ALIGN_PARENT_LEFT)
90-
titleParams.addRule(ALIGN_PARENT_RIGHT)
91-
textView.layoutParams = titleParams
92-
addView(textView)
93-
94-
//右边图标
95-
if (isShowRight) {
96-
val rightImageParams = LayoutParams(iconSize, iconSize)
97-
val rightImageView = ImageView(context)
98-
rightImageView.setImageResource(rightImageRes)
99-
rightImageView.scaleType = ImageView.ScaleType.CENTER_INSIDE
100-
rightImageParams.rightMargin = textMargin
101-
rightImageParams.addRule(CENTER_VERTICAL, TRUE)
102-
rightImageParams.addRule(ALIGN_PARENT_END, TRUE)
103-
addView(rightImageView, rightImageParams)
104-
rightImageView.setOnClickListener {
105-
listener?.onRightClick()
106-
}
107-
}
47+
//文字
48+
binding.titleView.text = title
49+
binding.titleView.textSize = if (isSmallerTitle) {
50+
16f
51+
} else {
52+
18f
10853
}
109-
}
54+
binding.titleView.setTextColor(titleColor)
11055

111-
/**
112-
* 设置View高度
113-
* */
114-
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
115-
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
116-
val widthSpecSize = MeasureSpec.getSize(widthMeasureSpec)
117-
setMeasuredDimension(widthSpecSize, titleHeight)
56+
//右边图标
57+
if (isShowRight) {
58+
binding.rightButton.setImageResource(rightImageRes)
59+
binding.rightButton.setOnClickListener {
60+
listener?.onRightClick()
61+
}
62+
}
11863
}
11964

12065
override fun onDraw(canvas: Canvas) {
@@ -127,15 +72,15 @@ class TitleBarView constructor(context: Context, attrs: AttributeSet) :
12772
* 动态设置标题
12873
* */
12974
fun setTitle(title: String) {
130-
textView.text = title
75+
binding.titleView.text = title
13176
invalidate()
13277
}
13378

13479
/**
13580
* 获取当前显示标题文字
13681
* */
13782
fun getTitle(): String {
138-
return textView.text.toString()
83+
return binding.titleView.text.toString()
13984
}
14085

14186
private var listener: OnClickListener? = null
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)