iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android TabLayout总结
  • 476
分享到

Android TabLayout总结

androidTabLayout 2023-09-04 12:09:49 476人浏览 泡泡鱼
摘要

文章目录 Android TabLayout总结基本使用添加图标、隐藏下划线自定义下划线、添加分割线设置角标圆角样式自定义View+Lottile代码下载 Android TabLay

文章目录

Android TabLayout总结

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是否选中

基本使用

在这里插入图片描述

TabLayout样式:

<style name="MyTabLayoutStyle">    "android:textSize">16sp    "android:textStyle">nORMal    "textAllCaps">falsestyle>

XML布局:

<com.Google.android.material.tabs.TabLayout                android:id="@+id/tabLayout01"                android:layout_width="match_parent"                android:layout_height="wrap_content"                app:tabIndicatorColor="@color/red"                app:tabIndicatorFullWidth="false"                app:tabMode="fixed"                app:tabSelectedTextColor="@color/red"                app:tabTextAppearance="@style/MyTabLayoutStyle"                app:tabTextColor="@color/black" /><androidx.viewpager2.widget.ViewPager2           android:id="@+id/viewPager2"           android:layout_width="match_parent"           android:layout_height="match_parent" />

代码:

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()

添加图标、隐藏下划线

在这里插入图片描述

XML布局:

<com.google.android.material.tabs.TabLayout                android:id="@+id/tabLayout02"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="10dp"                app:tabIndicatorHeight="0dp"                app:tabMode="fixed"                app:tabSelectedTextColor="@color/color_main"                app:tabTextAppearance="@style/MyTabLayoutStyle"                app:tabTextColor="@color/grey"                 app:tabIconTint="@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))}

自定义下划线、添加分割线

在这里插入图片描述

自定义下划线:

<layer-list xmlns:android="Http://schemas.android.com/apk/res/android">    <item        android:width="15dp"        android:height="5dp"        android:gravity="center">        <shape android:shape="rectangle">            <corners android:radius="10dp" />        shape>    item>layer-list>

自定义分割线:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item          android:width="5dp"          android:height="5dp"          android:gravity="center">        <shape android:shape="oval">            <solid android:color="@color/green" />        shape>    item>layer-list>

XML布局:

<com.google.android.material.tabs.TabLayout                android:id="@+id/tabLayout03"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="10dp"                app:tabIndicator="@drawable/tab_indicator"                app:tabIndicatorColor="@color/green"                app:tabIndicatorFullWidth="false"                app:tabMode="fixed"                app:tabSelectedTextColor="@color/green"                app:tabTextAppearance="@style/MyTabLayoutStyle"                app:tabTextColor="@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()

设置角标

在这里插入图片描述

XML布局:

代码:

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()

圆角样式

在这里插入图片描述

tab_bg_shape

<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="@color/color_main" />    <corners android:radius="100dp" />shape>

tab_indicator_shape

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:bottom="1dp"        android:gravity="center"        android:left="1dp"        android:right="1dp"        android:top="1dp">        <shape android:shape="rectangle">            <solid android:color="@color/white" />            <corners android:radius="100dp" />            <size android:height="40dp" />        shape>    item>layer-list>

XML布局:

    <com.google.android.material.tabs.TabLayout        android:id="@+id/tabLayout05"        android:layout_width="wrap_content"        android:layout_height="42dp"        android:layout_gravity="center"        android:layout_marginTop="10dp"        android:background="@drawable/tab_bg_shape"        app:tabIndicator="@drawable/tab_indicator_shape"        app:tabIndicatorColor="@color/white"        app:tabIndicatorFullWidth="true"        app:tabMinWidth="80dp"        app:tabMode="fixed"        app:tabSelectedTextColor="@color/color_main"        app:tabTextAppearance="@style/MyTabLayoutStyle"        app:tabTextColor="@color/black" />

自定义View+Lottile

在这里插入图片描述

XML布局:

item_tab

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:orientation="vertical">    <com.airbnb.lottie.LottieAnimationView        android:id="@+id/tab_img"        android:layout_width="30dp"        android:layout_height="30dp" />    <TextView        android:id="@+id/tab_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="14sp" />LinearLayout>

代码:

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<String, Int>(    "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<LottieAnimationView>(R.id.tab_img).apply {                      setAnimation(map[s]!!)                      setColorFilter(Color.BLUE)                  }                  val text = view.findViewById<TextView>(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<LottieAnimationView>(R.id.tab_img)        val text = it.findViewById<TextView>(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<LottieAnimationView>(R.id.tab_img)        val text = it.findViewById<TextView>(R.id.tab_text)        if (image.isAnimating) image.cancelAnimation()        image.progress = 0F        setLottieColor(image, false)        text.setTextColor(ContextCompat.getColor(mContext, R.color.black))    }}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<ColorFilter>(filter)        it.addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback)    }}

代码下载

来源地址:https://blog.csdn.net/qq_14876133/article/details/127844487

--结束END--

本文标题: Android TabLayout总结

本文链接: https://www.lsjlt.com/news/393553.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android TabLayout总结
    文章目录 Android TabLayout总结基本使用添加图标、隐藏下划线自定义下划线、添加分割线设置角标圆角样式自定义View+Lottile代码下载 Android TabLay...
    99+
    2023-09-04
    android TabLayout
  • Android CountDownTimer案例总结
    目录一、概述二、API三、基本使用方法四、使用注意一、概述 项目中经常用到倒计时的功能,比如说限时抢购,手机获取验证码等等。而google官方也帮我们封装好了一个类:CountDow...
    99+
    2024-04-02
  • Android之 弹框总结
    一 简介 1 弹框即浮与页面之上的窗口,如键盘弹框,吐司弹框,确认弹框,下拉选择框,应用悬浮框等 2 弹框控件也很多,比如常用的Spinner,Dialog,Toast,PopWindow等,以及新增的SnackBar,DialogFrag...
    99+
    2023-09-04
    android
  • Android之 动画总结
    一 动画种类 1 动画在Android中运用也非常广泛,如点击按钮,加载框,Activity的转场等都有动画的身影 2 常用的动画有以下以下几种 逐帧动画【Frame Animation】,即顺序播放事先准备的图片 补间动画【Tween A...
    99+
    2023-09-27
    android 动画
  • Android TabLayout选项卡如何使用
    这篇“Android TabLayout选项卡如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android...
    99+
    2023-07-05
  • android Retrofit2+okHttp3使用总结
    使用前准备Build.gradle文件配置dependencies配置compile 'com.squareup.retrofit2:retrofit:2.0.0'compile 'com.squareup.retrofit2:conver...
    99+
    2023-05-31
    okhttp retrofit roi
  • Android ViewModel的使用总结
    目录基本使用 MainRepositoryMainViewModelMainActivityViewModel 相关问题是高频面试题。主要源于它是 MVVM 架构模式的重要组件,并且...
    99+
    2024-04-02
  • Android中Service的全面总结
    全面总结Android Service的使用方法,具体内容如下1、Service的种类按运行地点分类:其实remote服务还是很少见的,并且一般都是系统服务。按运行类型分类:有同学可能会问,后台服务我们可以自己创建 ONGOING 的 No...
    99+
    2023-05-31
    android service roi
  • Android TabLayout 自定义样式及使用详解
    目录基本使用XML静态设置TabItem联动ViewPager2动态设置TabItem1. Activity布局代码2. 创建三个Fragment给ViewPager2设置3. Fr...
    99+
    2024-04-02
  • Android Notification使用方法总结
    Android Notification使用方法总结一. 基本使用1.构造notification NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(a...
    99+
    2023-05-30
    android notification roi
  • Android数据结构全面总结分析
    前言 这次算一个总结,我们平时都会用到各种各样的数据结构,但是可能从未看过它们内部是如何去实现的。只有了解了它们内部大概的一个实现原理,才能在不同的场景中能选出最适合这个场景的数据结...
    99+
    2022-12-08
    Android 数据结构 Android 数据结构总结分析
  • Android Insets相关知识总结
    目录什么是Insets?Insets相关类InsetsStateInsetsStateControllerInsetsSourceInsetsSourceConsumer(ImeIn...
    99+
    2024-04-02
  • Android onCreateOptionsMenu的使用方法总结
    Android onCreateOptionsMenu的使用方法总结任何一款软件都少不了对“菜单”的使用。在Android下,每一个activity都捆绑了一个Menu,要想定义和使用菜单,都必须在Activity下进行操作,复写onCre...
    99+
    2023-05-30
    android oncreateoptionsmenu roi
  • Android ButtonOnClick事件的写法总结
    Android ButtonOnClick事件的写法总结假设layout里有三个Button吧,id分别是 button_1 ,button_2 , button_3之前一直都知道有两种onClick写法:button_1.setOnCli...
    99+
    2023-05-30
    android buttononclick roi
  • Android Notification的多种用法总结
    Android Notification的多种用法总结我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问...
    99+
    2023-05-31
    android notification roi
  • Android字体相关知识总结
    目录一、Android 默认字体介绍二、textStyle三、typeface四、fontFamily 五、textStyle,typeface,fontFamily 三者...
    99+
    2024-04-02
  • Android WebView的使用方法总结
     Android WebView的使用方法  Android app打开H5页一般要实现如下需求:打开指定url网页;2、点击链接可以跳转到下一页,并更新标题;3、按back键或左箭头可以返回上一页;4、当webview...
    99+
    2023-05-30
    android webview roi
  • android中adb命令最全总结
    目录 一、查看adb版本 二、查看已经连接的设备 三、获取手机序列号 四、查看手机设别型号 五、查看手机分辨率 六、获取手机的mac地址 七、查看日志 八、查看电池信息 九...
    99+
    2024-04-02
  • Android 13 以太网开发总结
    Android 13 以太网开发总结 前言 相较于Android12,Android13将以太网相关功能整合到ConnectivityService里,将以太网的核心源码从framework上移到pa...
    99+
    2023-09-27
    android java 网络
  • Android查看内存命令总结
    ① adb shell df -h 查看分区情况 ② du -sh * 查询目录的磁盘使用空间 ③ dumpsys devicestoragemonitor 显示设备内存信息 ④ pm list...
    99+
    2023-08-31
    Android 查看内存命令
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作