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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分析Android App中內(nèi)置換膚功能的實(shí)現(xiàn)方式
這篇文章主要介紹了Android App中內(nèi)置換膚功能的實(shí)現(xiàn)方式,文中舉了一個(gè)類似QQ空間中換膚方式的例子作為說(shuō)明,需要的朋友可以參考下2016-02-02android特賣列表倒計(jì)時(shí)卡頓問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了android特賣列表倒計(jì)時(shí)卡頓問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09A07_TimePicker & DatePicker & AnalogClock & Digi
本文將帶領(lǐng)大家一起學(xué)習(xí)時(shí)間日期和時(shí)鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈2013-06-06android獲取相冊(cè)圖片和路徑的實(shí)現(xiàn)方法
這篇文章主要介紹了android獲取相冊(cè)圖片和路徑的實(shí)現(xiàn)方法,本文介紹的是Android4.4后的方法,感興趣的小伙伴們可以參考一下2016-04-04Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對(duì)話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計(jì)風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10Android基于OpenCV實(shí)現(xiàn)Harris角點(diǎn)檢測(cè)
角點(diǎn)就是極值點(diǎn),即在某方面屬性特別突出的點(diǎn)。當(dāng)然,你可以自己定義角點(diǎn)的屬性(設(shè)置特定熵值進(jìn)行角點(diǎn)檢測(cè))。角點(diǎn)可以是兩條線的交叉處,也可以是位于相鄰的兩個(gè)主要方向不同的事物上的點(diǎn)。本文介紹如何基于OpenCV實(shí)現(xiàn)Harris角點(diǎn)檢測(cè)2021-06-06Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn)
這篇文章主要介紹了Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Android實(shí)現(xiàn)View拖拽跟隨手指移動(dòng)效果
這篇文章主要介紹了Android實(shí)現(xiàn)View拖拽跟隨手指移動(dòng)效果,主要使用setTranslationX() 和setTranslationY() 屬性方法實(shí)現(xiàn)的,需要的朋友參考下吧2017-08-08