Android自定义view实现标签栏功能(只支持固定两个标签)
更新时间:2020年06月07日 11:08:31 作者:安卓007
这篇文章主要介绍了Android自定义view实现标签栏(只支持固定两个标签),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
实现效果图
主要代码
完整源代码
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标签栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android利用ViewPager实现可滑动放大缩小画廊效果
这篇文章主要介绍了Android利用ViewPager实现可滑动放大缩小画廊效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-08-08Android 深入探究自定义view之事件的分发机制与处理详解
对于安卓程序员来说,自定义view简直不要太重要,毕竟有很多功能,譬如圆形头像这些,用单纯的原生非常难以实现,而用自定义view,简直分分钟2021-11-11Android使用Item Swipemenulistview实现仿QQ侧滑删除功能
大家都用过QQ,肯定有人好奇QQ滑动删除Item的效果是怎样实现的,其实我们使用Swipemenulistview就可以简单的实现。这篇文章主要介绍了Android使用ItemSwipemenulistview实现仿QQ侧滑删除功能,需要的朋友可以参考下2017-02-02Android 百度地图定位实现仿钉钉签到打卡功能的完整代码
这篇文章主要介绍了Android 百度地图定位实现仿钉钉签到打卡功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-04-04
最新评论