时间:2021-05-20
实现效果图
主要代码
完整源代码
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标签栏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android自定义View的构造函数自定义View是Android中一个常见的需求,每个自定义的View都需要实现三个基本的构造函数,而这三个构造函数又有两种
前面在学习鸿洋大神的一些自定义的View文章,看到了自定义ViewGroup实现浮动标签,初步看了下他的思路以及结合自己的思路完成了自己的浮动标签的自定义Vie
看了Android版QQ的自定义头像功能,决定自己实现,随便熟悉下android绘制和图片处理这一块的知识。先看看效果:思路分析:这个效果可以用两个View来完
本文实例为大家分享了Android实现底部切换标签的具体代码,供大家参考,具体内容如下实现底部通用切换标签,嵌套Fragment,方便自定义布局自定义控件:wi
dynamic_sidebar()函数用来支持自定义sidebar侧边栏,可以自定义Widget插件,比如为侧边栏添加最近文章,文章归档,最新评论,标签云,搜索