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 用HttpURLConnection訪問(wèn)網(wǎng)絡(luò)的方法
下面小編就為大家分享一篇Android 用HttpURLConnection訪問(wèn)網(wǎng)絡(luò)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android實(shí)現(xiàn)字母導(dǎo)航控件的示例代碼
這篇文章主要介紹了通過(guò)自定義View實(shí)現(xiàn)字母導(dǎo)航控件的示例代碼,文中的實(shí)現(xiàn)過(guò)程講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的可以學(xué)習(xí)一下2022-01-01
分享Android開(kāi)發(fā)自學(xué)筆記之AndroidStudio常用功能
這篇文章主要給大家分享Android開(kāi)發(fā)自學(xué)筆記之AndroidStudio常用功能的相關(guān)資料,需要的朋友可以參考下2015-12-12
如何在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果
本篇文章是對(duì)在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android 自定義閃屏頁(yè)廣告倒計(jì)時(shí)view效果
這篇文章主要介紹了Android 自定義閃屏頁(yè)廣告倒計(jì)時(shí)view效果,需要的朋友可以參考下2017-05-05
接口對(duì)象的實(shí)例化在接口回調(diào)中的使用方法
下面小編就為大家?guī)?lái)一篇接口對(duì)象的實(shí)例化在接口回調(diào)中的使用方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

