@@ -3,27 +3,25 @@ package com.pengxh.kt.lite.widget
3
3
import android.content.Context
4
4
import android.graphics.Canvas
5
5
import android.graphics.Color
6
- import android.text.TextUtils
7
6
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
12
9
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
15
11
16
12
17
13
/* *
18
14
* 界面顶部标题栏
19
15
* */
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) {
22
18
23
- private val titleHeight = 45 .dp2px(context)
24
- private var textView : TextView
19
+ private val kTag = " TitleBarView "
20
+ private var binding : WidgetViewTitleBarBinding
25
21
26
22
init {
23
+ binding = WidgetViewTitleBarBinding .inflate(LayoutInflater .from(context), this , true )
24
+
27
25
val type = context.obtainStyledAttributes(attrs, R .styleable.TitleBarView )
28
26
val leftImageRes = type.getResourceId(
29
27
R .styleable.TitleBarView_tbv_left_image , R .drawable.ic_title_left
@@ -35,86 +33,33 @@ class TitleBarView constructor(context: Context, attrs: AttributeSet) :
35
33
val isShowRight = type.getBoolean(R .styleable.TitleBarView_tbv_show_right_image , false )
36
34
val title = type.getText(R .styleable.TitleBarView_tbv_text )
37
35
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 )
40
37
type.recycle()
41
38
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()
73
44
}
45
+ }
74
46
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
108
53
}
109
- }
54
+ binding.titleView.setTextColor(titleColor)
110
55
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
+ }
118
63
}
119
64
120
65
override fun onDraw (canvas : Canvas ) {
@@ -127,15 +72,15 @@ class TitleBarView constructor(context: Context, attrs: AttributeSet) :
127
72
* 动态设置标题
128
73
* */
129
74
fun setTitle (title : String ) {
130
- textView .text = title
75
+ binding.titleView .text = title
131
76
invalidate()
132
77
}
133
78
134
79
/* *
135
80
* 获取当前显示标题文字
136
81
* */
137
82
fun getTitle (): String {
138
- return textView .text.toString()
83
+ return binding.titleView .text.toString()
139
84
}
140
85
141
86
private var listener: OnClickListener ? = null
0 commit comments