# Android TabLayout总结 站点:答源(https://www.dandroid.cn/) 原文:https://www.dandroid.cn/archives/20333 Canonical:https://www.dandroid.cn/archives/20333 作者:admin 发布日期:2023-09-15 最后更新:2023-09-15 10:43 分类:教程 内容类型:post 说明:本页是原文的纯文本镜像,供大模型与检索器完整读取;不含导航、广告、付费框。引用请以上方「原文」链接为准。 ## 正文 TabLayout基本属性: - background:背景颜色 - tabTextColor:默认文本颜色 - tabSelectedTextColor:选中文本颜色 - tabIndicatorColor:下划线颜色 - tabIndicatorFullWidth:下划线是否填充宽度 - tabIndicator:指示器 - tabMode:滚动模式 - tabTextAppearance:文本样式,如字体大小、粗细、大小写 - tabIndicatorHeight:下划线高度。设置为0时,则不显示 - tabMaxWidth:tab最大宽度 - tabMinWidth:tab最小宽度 TabLayout.Tab基本属性: - setCustomView:自定义View - setIcon:设置图标 - setText:设置文本 - getOrCreateBadge:获取badge - removeBadge:移除badge - select:选中tab - isSelected:判断tab是否选中 基本使用 viewPager2.adapter = object : FragmentStateAdapter(this@TabLayoutActivity) { override fun getItemCount(): Int { return fragments.size } override fun createFragment(position: Int): Fragment { return fragments[position] } } TabLayoutMediator( tabLayout01, viewPager2, object : TabLayoutMediator.TabConfigurationStrategy { override fun onConfigureTab(tab: TabLayout.Tab, position: Int) { tab.text = titles[position] } }).attach() TabLayoutMediator( tabLayout02, viewPager2, object : TabLayoutMediator.TabConfigurationStrategy { override fun onConfigureTab(tab: TabLayout.Tab, position: Int) { tab.text = titles[position] } }).attach() for (i in 0..tabLayout02.tabCount) { tabLayout02.getTabAt(i)?.setIcon(drawables[i]) } tabLayout02.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { override fun onTabSelected(tab: TabLayout.Tab?) { tab?.icon?.selected() } override fun onTabUnselected(tab: TabLayout.Tab?) { tab?.icon?.unselected() } override fun onTabReselected(tab: TabLayout.Tab?) { } }) val defaultTab = tabLayout02.getTabAt(defaultIndex) defaultTab?.select() defaultTab?.icon?.selected() //图片选中状态 fun Drawable.selected() { this.setTint(ContextCompat.getColor(mContext, R.color.color_main)) } //图片未选中状态 fun Drawable.unselected() { this.setTint(ContextCompat.getColor(mContext, R.color.grey)) } TabLayoutMediator( tabLayout02, viewPager2, object : TabLayoutMediator.TabConfigurationStrategy { override fun onConfigureTab(tab: TabLayout.Tab, position: Int) { tab.text = titles[position] } }).attach() for (i in 0..tabLayout02.tabCount) { tabLayout02.getTabAt(i)?.setIcon(drawables[i]) } tabLayout02.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { override fun onTabSelected(tab: TabLayout.Tab?) { tab?.icon?.selected() } override fun onTabUnselected(tab: TabLayout.Tab?) { tab?.icon?.unselected() } override fun onTabReselected(tab: TabLayout.Tab?) { } }) val defaultTab = tabLayout02.getTabAt(defaultIndex) defaultTab?.select() defaultTab?.icon?.selected() //图片选中状态 fun Drawable.selected() { this.setTint(ContextCompat.getColor(mContext, R.color.color_main)) } //图片未选中状态 fun Drawable.unselected() { this.setTint(ContextCompat.getColor(mContext, R.color.grey)) } TabLayoutMediator( tabLayout03, viewPager2, object : TabLayoutMediator.TabConfigurationStrategy { override fun onConfigureTab(tab: TabLayout.Tab, position: Int) { tab.text = titles[position] } }).attach() for (i in 0..tabLayout03.tabCount) { val linearLayout = tabLayout03.getChildAt(i) as? LinearLayout linearLayout?.apply { showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE dividerDrawable = ContextCompat.getDrawable(mContext, R.drawable.tab_divider) dividerPadding = 2.dp } } val defaultTab = tabLayout03.getTabAt(defaultIndex) defaultTab?.select() TabLayoutMediator( tabLayout04, viewPager2, object : TabLayoutMediator.TabConfigurationStrategy { override fun onConfigureTab(tab: TabLayout.Tab, position: Int) { tab.text = titles[position] } }).attach() //数字角标 tabLayout04.getTabAt(1)?.let { it.orCreateBadge.apply { backgroundColor = Color.RED maxCharacterCount = 3 number = 99999 badgeTextColor = Color.WHITE } } //红点 tabLayout04.getTabAt(2)?.let { it.orCreateBadge.backgroundColor = ContextCompat.getColor(this, R.color.orange) } val defaultTab = tabLayout04.getTabAt(defaultIndex) defaultTab?.select() viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(position: Int) { super.onPageSelected(position) tabLayout06.getTabAt(position)?.select() } }) val layoutInflate = LayoutInflater.from(mContext) val map = mapOf( "apple" to R.raw.apple, "heart" to R.raw.heart, "sun_moon" to R.raw.sun_moon, "pizza" to R.raw.pizza ) map.keys.forEach { s: String -> val tab = tabLayout06.newTab() val view = layoutInflate.inflate(R.layout.item_tab, null) val image = view.findViewById(R.id.tab_img).apply { setAnimation(map[s]!!) setColorFilter(Color.BLUE) } val text = view.findViewById(R.id.tab_text).apply { text = s } tab.customView = view tabLayout06.addTab(tab) } tabLayout06.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { override fun onTabSelected(tab: TabLayout.Tab?) { tab?.selected() tab?.let { viewPager2.currentItem = it.position } } override fun onTabUnselected(tab: TabLayout.Tab?) { tab?.unselect() } override fun onTabReselected(tab: TabLayout.Tab?) { } }) val defaultTab = tabLayout06.getTabAt(defaultIndex) defaultTab?.select() defaultTab?.selected() /** * 选中状态 */ fun TabLayout.Tab.selected() { this.customView?.let { val image = it.findViewById(R.id.tab_img) val text = it.findViewById(R.id.tab_text) if (!image.isAnimating) image.playAnimation() setLottieColor(image, true) text.setTextColor(ContextCompat.getColor(mContext, R.color.blue)) } } /** * 未选中状态 */ fun TabLayout.Tab.unselect() { this.customView?.let { val image = it.findViewById(R.id.tab_img) val text = it.findViewById(R.id.tab_text) if (image.isAnimating) image.cancelAnimation() image.progress = 0F setLottieColor(image, false) text.setTextColor(ContextCompat.getColor(mContext, R.color.black)) } } /** * set lottie icon color */ private fun setLottieColor(imageView: LottieAnimationView?, isSelected: Boolean) { imageView?.let { val color = if (isSelected) R.color.blue else R.color.black val csl = AppCompatResources.getColorStateList(this@TabLayoutActivity, color) val filter = SimpleColorFilter(csl.defaultColor) val keyPath = KeyPath("**") val callback = LottieValueCallback(filter) it.addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback) } } --- 由 答源 DAN 底座 1.4.1 生成 · 2026-11-24T12:21:30+00:00