溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android自定義view實現(xiàn)標(biāo)簽欄功能的實例解析

發(fā)布時間:2020-07-18 11:03:43 來源:億速云 閱讀:173 作者:小豬 欄目:移動開發(fā)

這篇文章主要講解了Android自定義view實現(xiàn)標(biāo)簽欄功能的實例解析,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

實現(xiàn)效果圖

Android自定義view實現(xiàn)標(biāo)簽欄功能的實例解析

主要代碼

完整源代碼

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

看完上述內(nèi)容,是不是對Android自定義view實現(xiàn)標(biāo)簽欄功能的實例解析有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI