网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android自定义view实现标签栏功能的实例解析

这篇文章主要讲解了Android自定义view实现标签栏功能的实例解析,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

创新互联公司专注于当阳企业网站建设,响应式网站,成都商城网站开发。当阳网站建设公司,为当阳等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

实现效果图

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
 }
}

看完上述内容,是不是对Android自定义view实现标签栏功能的实例解析有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


网站题目:Android自定义view实现标签栏功能的实例解析
浏览路径:http://bjjierui.cn/article/iisjsd.html

其他资讯