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 } }