android 固定标签栏,Android自定义view实现标签栏功能(只支持固定两个标签)

实现效果图

151e06f5d1678b2d232363be7bf966da.png

主要代码

完整源代码

class TabView(context: Context, attributeSet: AttributeSet?) : LinearLayout(context, attributeSet) {

private lateinit var firstTab: View

private lateinit var secondTab: View

private val firstTabIndex = 0

private val secondTabIndex = 1

private var selectedTab = firstTabIndex

private val textSize = 20f

private val bottomSplitColor = "#FA871E"

private val centerSplitColor = "#666666"

private val bottomSplitWidth = 50

private val bottomSplitHeight = 4

private val centerSplitWidth = 1

private val centerSplitHeight = 40

private lateinit var mOnSwitchListener: OnSwitchListener

fun initTabs(

firstTabText: String,

secondTabText: String,

selectedIndex: Int,

onSwitchListener: OnSwitchListener

) {

mOnSwitchListener = onSwitchListener

setOrientation()

firstTab = addTab(firstTabText)

addCenterSplit()

secondTab = addTab(secondTabText)

selectTab(selectedIndex)

setOnClickListener { switchTab() }

}

interface OnSwitchListener {

fun onSwitched(selectedIndex: Int)

}

private fun selectTab(tabIndex: Int) {

if (tabIndex == firstTabIndex) {

firstTab.visibility = View.VISIBLE

secondTab.visibility = View.INVISIBLE

} else {

firstTab.visibility = View.INVISIBLE

secondTab.visibility = View.VISIBLE

}

selectedTab = tabIndex

}

private fun switchTab() {

if (selectedTab == firstTabIndex) {

selectTab(secondTabIndex)

} else {

selectTab(firstTabIndex)

}

mOnSwitchListener.onSwitched(selectedTab)

}

private fun setOrientation() {

orientation = HORIZONTAL

}

private fun getBottomSplitView(): View {

val view = View(context)

view.setBackgroundColor(Color.parseColor(bottomSplitColor))

return view

}

private fun getBottomSplitLayoutParams(): LayoutParams {

val layoutParams = LayoutParams(bottomSplitWidth, bottomSplitHeight)

layoutParams.setMargins(3, 3, 3, 3)

layoutParams.gravity = Gravity.CENTER_HORIZONTAL

return layoutParams

}

private fun addCenterSplit() {

val view = View(context)

view.setBackgroundColor(Color.parseColor(centerSplitColor))

addView(view, getCenterSplitLayoutParams())

}

private fun getCenterSplitLayoutParams(): LayoutParams {

val layoutParams = LayoutParams(centerSplitWidth, centerSplitHeight)

layoutParams.setMargins(3, 0, 3, 0)

layoutParams.gravity = Gravity.CENTER_VERTICAL

return layoutParams

}

private fun addTab(text: String): View {

var linearLayout = LinearLayout(context)

linearLayout.orientation = VERTICAL

val textView = getTextView(text)

linearLayout.addView(

textView,

LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

)

val splitView = getBottomSplitView()

linearLayout.addView(splitView, getBottomSplitLayoutParams())

addView(linearLayout, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))

return splitView

}

private fun getTextView(text: String): TextView {

val textView = TextView(context)

textView.text = text

textView.setPadding(10, 10, 10, 10)

textView.textSize = textSize

return textView

}

}

总结

到此这篇关于Android自定义view实现标签栏功能(只支持固定两个标签)的文章就介绍到这了,更多相关android自定义view标签栏内容请搜索编程圈以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程圈!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值