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. XML入門精解之結(jié)構(gòu)與語(yǔ)法2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享4. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理5. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識(shí))6. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. PHP session反序列化漏洞超詳細(xì)講解9. 解析原生JS getComputedStyle10. css代碼優(yōu)化的12個(gè)技巧
