av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Android自定義view實現標簽欄功能(只支持固定兩個標簽)

瀏覽:80日期:2022-09-23 17:57:26

實現效果圖

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

https://gitee.com/cxyzy1/custTabView

總結

到此這篇關于Android自定義view實現標簽欄功能(只支持固定兩個標簽)的文章就介紹到這了,更多相關android自定義view標簽欄內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 极品白嫩少妇无套内谢 | 伊人国产女 | 久久一区二区三区四区 | 中国一级黄 | 亚洲欧美综合 | 超碰91在线| 欧美一区二区三区在线视频 | 五月播播| 福利视频免费观看 | 日韩在线观看免费 | 午夜精品福利视频 | 日本精品视频在线 | 偷偷操网站 | 成 人 黄 色 片 在线播放 | 91小视频在线观看 | 欧美三级韩国三级日本三斤在线观看 | 亚洲精品www久久久久久广东 | 亚洲精品在线免费 | 色婷婷国产 | 国产精品一级二级三级 | 国产一级生活片 | 国产午夜精品一区二区三区四区 | xxxx性欧美 | 男女啪啪免费视频 | 四虎影院www | 伊人久久av| 91一级片 | 日韩亚洲一区二区 | 人人干人人艹 | 日本欧美久久久久免费播放网 | 天天干夜夜撸 | 韩日一级片 | 久久福利社 | 国产成人网 | 999毛片| 一级特黄妇女高潮 | 日本精品国产 | 超碰99在线 | 在线视频h | 国产亚洲视频在线观看 | 九月丁香婷婷 |