Android自定義view實(shí)現(xiàn)標(biāo)簽欄功能(只支持固定兩個(gè)標(biāo)簽)
實(shí)現(xiàn)效果圖
主要代碼
完整源代碼
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
總結(jié)
到此這篇關(guān)于Android自定義view實(shí)現(xiàn)標(biāo)簽欄功能(只支持固定兩個(gè)標(biāo)簽)的文章就介紹到這了,更多相關(guān)android自定義view標(biāo)簽欄內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. css進(jìn)階學(xué)習(xí) 選擇符2. python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能3. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))4. python爬蟲beautifulsoup解析html方法5. python web框架的總結(jié)6. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式7. 解決python logging遇到的坑 日志重復(fù)打印問(wèn)題8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. Python基礎(chǔ)之numpy庫(kù)的使用10. 詳解Python模塊化編程與裝飾器
