服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android自定义view实现标签栏功能(只支持固定两个标签)

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

2022-12-29 15:59安卓007 Android

这篇文章主要介绍了Android自定义view实现标签栏(只支持固定两个标签),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

实现效果图

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

主要代码

完整源代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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标签栏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/yinxing2008/article/details/106571764

延伸 · 阅读

精彩推荐