博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓自定义view实现标签栏(只支持固定两个标签)
阅读量:1894 次
发布时间:2019-04-26

本文共 3477 字,大约阅读时间需要 11 分钟。

实现效果图

主要代码

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

完整源代码

转载地址:http://rgadf.baihongyu.com/

你可能感兴趣的文章
手敲一个前后端分离项目——成果演示
查看>>
Linux——基础命令篇
查看>>
EL表达式、JSTL标签库、文件上传和下载
查看>>
Cookie、Session
查看>>
表单重复提交
查看>>
Filter
查看>>
微服务架构实施原理详解
查看>>
必须了解的mysql三大日志-binlog、redo log和undo log
查看>>
格式化字符串(%s,%d,%r)
查看>>
2021江西省数学建模二题
查看>>
2021江西省数学建模三题
查看>>
全连接层、卷积层、池化层
查看>>
ImageNet Classification with Deep Convolutional Neural Networks
查看>>
VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION-2014
查看>>
使用pytorch搭建AlexNet
查看>>
[系统安全] 三十一.恶意代码检测(1)恶意代码攻击溯源及恶意样本分析
查看>>
[Python从零到壹] 十.网络爬虫之Selenium爬取在线百科知识万字详解(NLP语料构造必备技能)
查看>>
[Python从零到壹] 十一.数据分析之Numpy、Pandas、Matplotlib和Sklearn入门知识万字详解(1)
查看>>
打破定式,突破屏障,走出自己的创意舒适区
查看>>
又一个程序员倒下-程序员防猝死指南
查看>>