前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android自定义view实现标签栏功能(只支持固定两个标签)

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

作者头像
砸漏
发布2020-10-16 10:08:50
2790
发布2020-10-16 10:08:50
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

实现效果图

主要代码

完整源代码

代码语言:javascript
复制
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
}
}

https://gitee.com/cxyzy1/custTabView

总结

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档