lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
package cn.sinata.xldutils.view
 
import android.app.Activity
import android.content.Context
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import android.text.InputType
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import cn.sinata.xldutils.R
import org.jetbrains.anko.*
import java.util.*
 
/**
 * 标题栏
 * Created by liaoxiang on 16/3/22.
 */
class TitleBar : FrameLayout {
 
    constructor(context: Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)
 
    private var hasLeft = true
    val leftView: TextView by lazy {
        textView {
            textSize = 14f
            textColor = ContextCompat.getColor(context, R.color.textColor)
            maxLines = 1
            ellipsize = TextUtils.TruncateAt.END
            maxWidth = dip(120)
            gravity = Gravity.CENTER
        }
    }
    val titleView: TextView by lazy {
        textView {
            textSize = 20f
            maxLines = 1
            ellipsize = TextUtils.TruncateAt.END
            gravity = Gravity.CENTER
            textColor = ContextCompat.getColor(context, R.color.white)
            typeface = Typeface.DEFAULT_BOLD
        }
    }
    private val rightViews = ArrayList<View>()
 
    init {
        //宽度全屏,高度46dp
        layoutParams = ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, dip(46))
        //添加leftView
        leftView
        val param = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT)
        param.bottomMargin = dip(4)
        param.topMargin = dip(4)
        param.gravity = Gravity.CENTER_VERTICAL
        showLeft(hasLeft)
        leftView.setPadding(dip(16), 0, dip(8), 0)
        leftView.layoutParams = param
        leftView.compoundDrawablePadding = dip(4)
        leftView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.back, 0, 0, 0)
        //添加TitleView
        titleView
        titleView.setPadding(dip(4), 0, dip(4), 0)
        val param1 = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT)
        param1.bottomMargin = dip(4)
        param1.topMargin = dip(4)
        titleView.compoundDrawablePadding = dip(4)
        param1.gravity = Gravity.CENTER
        titleView.layoutParams = param1
 
        //默认不可输入
//        titleView.inputType = InputType.TYPE_NULL
//        titleView.isEnabled = false
        setTitleColor(R.color.title_text)
        //默认白色
        titleView.backgroundDrawable = null
        //默认左键点击关闭页面
        leftView.setOnClickListener {
            (context as Activity).onBackPressed()
        }
    }
 
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
 
        var lw = 0
        if (hasLeft) {
            lw = leftView.measuredWidth
        }
        var rw = 0
        if (isHasRight) {
            for (view in rightViews) {
                if (view.visibility != View.GONE) {
                    val rightMargin = (view.layoutParams as FrameLayout.LayoutParams).rightMargin
                    val temp = rw
                    if (rightMargin != temp) {
                        (view.layoutParams as FrameLayout.LayoutParams).rightMargin = temp
                    }
                    rw += view.measuredWidth + 5
                }
            }
        }
        val leftMargin = (titleView.layoutParams as FrameLayout.LayoutParams).leftMargin
        val rightMargin = (titleView.layoutParams as FrameLayout.LayoutParams).rightMargin
        val newMargin = Math.max(lw, rw) + 5
//                System.err.println("onlayout-------$leftMargin--->$rightMargin")
        if (leftMargin != newMargin || rightMargin != newMargin) {
            (titleView.layoutParams as FrameLayout.LayoutParams).leftMargin = newMargin
            (titleView.layoutParams as FrameLayout.LayoutParams).rightMargin = newMargin
            titleView.requestLayout()
        }
    }
 
    fun showLeft(show: Boolean) {
        hasLeft = show
        leftView.visibility = if (show) View.VISIBLE else View.GONE
    }
 
    fun setCanEditable(canEditable: Boolean) {
        if (canEditable) {
            titleView.inputType = InputType.TYPE_CLASS_TEXT
            titleView.isEnabled = true
        }
    }
 
    fun setTitle(s: CharSequence) {
        titleView.setText(s)
    }
 
    fun setTitleColor(@ColorRes color: Int) {
        titleView.setTextColor(ContextCompat.getColor(context, color))
    }
 
    fun setTitle(s: CharSequence, size: Float) {
        titleView.setText(s)
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size)
    }
 
    fun addRightButton(title: String? = null, rightId: Int = 0, onClickListener: View.OnClickListener) {
        //        hasRight = true;
        val padding = dip(16)
        val drawablePadding = dip(5)
 
        val w = rightViews
                .filter { it.visibility != View.GONE }
                .sumBy { it.measuredWidth + 5 }
        val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT)
 
        params.rightMargin = w
        params.gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL
        val rightView = TextView(context)
        rightView.compoundDrawablePadding = drawablePadding
        if (title != null) {
            rightView.text = title
        }
        rightView.setTextColor(ContextCompat.getColor(context, R.color.textColor))
        rightView.setPadding(padding / 2, 0, padding, 0)
        rightView.gravity = Gravity.CENTER_VERTICAL
        rightView.layoutParams = params
        var right: Drawable? = null
        if (rightId > 0) {
            right = ContextCompat.getDrawable(context, rightId)
        }
        rightView.setCompoundDrawablesWithIntrinsicBounds(null, null, right, null)
        rightView.setOnClickListener(onClickListener)
        addView(rightView)
        rightViews.add(rightView)
    }
 
    /**
     * 设置右边第p个按钮(0开始)的文字,图片
     */
    fun setRightButton(position: Int = 0, title: String, resId: Int = 0) {
        if (isHasRight && rightViews.size > position && position >= 0) {
            (rightViews[position] as TextView).text = title
            if (resId > 0) {
                val drawable = ContextCompat.getDrawable(context, resId)
                (rightViews[position] as TextView).setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
            }
        }
    }
 
    fun getRightButton(position: Int): TextView? {
        if (rightViews.isEmpty()) {
            return null
        }
        val view = rightViews[position]
        if (view is TextView) {
            return view
        }
        return TextView(context)
    }
 
    fun hideRightButton(position: Int, hide: Boolean) {
        if (isHasRight && rightViews.size > position && position >= 0) {
            rightViews[position].visibility = if (hide) View.GONE else View.VISIBLE
        }
    }
 
    fun hideAllRightButton() {
        if (isHasRight) {
            for (view in rightViews) {
                view.visibility = View.GONE
            }
        }
    }
 
    fun showAllRightButton() {
        if (isHasRight) {
            for (view in rightViews) {
                view.visibility = View.VISIBLE
            }
        }
    }
 
    fun leftClick(l: ((v: View) -> Unit)) = leftView.setOnClickListener(l)
 
    private val isHasRight: Boolean
        get() = rightViews.size > 0
}