18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
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
package com.example.smart.widget
 
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.style.StyleSpan
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.example.smart.R
 
/**
 * desc:
 */
@SuppressLint("ViewConstructor")
class HomeTabView  @JvmOverloads constructor(
    context: Context,
    @DrawableRes icon: Int?,
    tabName: String?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attrs, defStyleAttr)  {
 
    private lateinit var mIcon: ImageView
    private lateinit var tv: TextView
 
    private var mContext: Context? = null
    private var mTabPosition = -1
 
    init {
        initView(context, icon, tabName)
    }
 
 
    private fun initView(context: Context, icon: Int?, tabName: String?) {
        orientation = VERTICAL
 
        if (icon != null) {
            mContext = context
            mIcon = ImageView(context)
            val size = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                27f,
                resources.displayMetrics
            ).toInt()
            val params = LayoutParams(size, size)
            params.gravity = Gravity.CENTER_HORIZONTAL
            mIcon.setPadding(0, 18, 0, 0)
            mIcon.setImageResource(icon)
            mIcon.layoutParams = params
            addView(mIcon)
        }
 
        if (tabName != null) {
            tv = TextView(context)
            tv.setTextColor(resources.getColor(R.color.gray_64))
            tv.textSize = 10f
            tv.setPadding(0, 5, 0, 12)
            tv.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
            val string = SpannableString(tabName)
 
            string.setSpan(StyleSpan(Typeface.BOLD), 0, string.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            tv.text = string
            addView(tv)
        }
 
    }
 
 
    fun setTabPosition(position: Int) {
        mTabPosition = position
        if (position == 0) {
            isSelected = true
        }
    }
 
 
    override fun setSelected(selected: Boolean) {
        super.setSelected(selected)
        if (selected) {
            tv.setTextColor(resources.getColor(R.color.txt_blue))
        } else {
            tv.setTextColor(resources.getColor(R.color.gray_64))
        }
    }
 
    fun getTabPosition(): Int {
        return mTabPosition
    }
}