欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-20 18:49:21
实现效果图主要代码完整源代码class tabview(context: context, attributeset: attributeset?) : linearlayout(contex...

实现效果图

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标签栏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!