• Android TabLayout的使用与总结


    最近的新项目里老是会用到tablayout,便想着自己也总结下,加深印象,于是他来啦!

    一、什么是TabLayout?

    TabLayout是Android中的一个控件,常和ViewPager搭配使用,运用于tab切换viewpager,Google在升级了AndroidX之后,将TabLayout迁移到material包的com.google.android.material.tabs.TabLayout

    二、常规使用

    即TabLayout和viewpager(viewpager2也行)的搭配使用

    1.xml文件

    这是一个IM模块的界面,需要展示好友列表和聊天列表,使用的约束布局,tablayout用于切换viewpager和标题展示,最下面的一个图片是需求上的点击跳转搜索好友界面

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <layout xmlns:android="http://schemas.android.com/apk/res/android"//用layout作为父布局是因为databinding的原因
    3. xmlns:app="http://schemas.android.com/apk/res-auto">
    4. <androidx.constraintlayout.widget.ConstraintLayout
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:paddingTop="48dp"
    8. android:background="@color/black"
    9. >
    10. <com.google.android.material.tabs.TabLayout
    11. android:id="@+id/im_contact_tab"
    12. android:layout_width="0dp"
    13. android:layout_height="wrap_content"
    14. app:tabBackground="@color/black"
    15. app:tabIndicatorGravity="bottom"//在tab的底部
    16. app:tabIndicatorFullWidth="false"
    17. app:tabIndicator="@drawable/tab_indicator"//为了需求自定义的tabindicator
    18. app:tabIndicatorColor="#FFF65F2A"//颜色
    19. android:layout_marginEnd="60dp"
    20. app:tabSelectedTextColor="@color/white"//选中后文本颜色
    21. app:layout_constraintEnd_toStartOf="@id/im_add_img"
    22. app:layout_constraintStart_toStartOf="parent"
    23. app:layout_constraintTop_toTopOf="parent"
    24. app:tabTextColor="#ccffffff" />
    25. <androidx.viewpager2.widget.ViewPager2
    26. android:id="@+id/im_contact_viewpager"
    27. android:layout_width="match_parent"
    28. android:layout_height="0dp"
    29. app:layout_constraintTop_toBottomOf="@id/im_contact_tab"
    30. app:layout_constraintBottom_toBottomOf="parent">
    31. </androidx.viewpager2.widget.ViewPager2>
    32. <androidx.appcompat.widget.AppCompatImageView
    33. android:layout_width="24dp"
    34. android:layout_height="24dp"
    35. android:layout_marginEnd="16dp"
    36. android:id="@+id/im_add_img"
    37. android:src="@mipmap/ic_add_friend_new"
    38. app:layout_constraintEnd_toEndOf="parent"
    39. app:layout_constraintStart_toEndOf="@id/im_contact_tab"
    40. app:layout_constraintTop_toTopOf="@id/im_contact_tab"
    41. app:layout_constraintBottom_toBottomOf="@id/im_contact_tab"
    42. />
    43. </androidx.constraintlayout.widget.ConstraintLayout>
    44. </layout>

    2.实现代码(kotlin)

    1. class IMContactFragment : BaseBindFragment<FgContactLayoutBinding>() {
    2. //使用的databinding
    3. override val layout: Int
    4. get() = R.layout.fg_contact_layout
    5. private val viewPager: ViewPager2 by lazy { mBinding.imContactViewpager }
    6. private val tabLayout: TabLayout by lazy { mBinding.imContactTab }
    7. var fragments = mutableListOf<Fragment>()//fragment列表
    8. var titles = mutableListOf("Friends","Message")//标题列表
    9. companion object {
    10. fun getInstance(): IMContactFragment {
    11. return IMContactFragment()
    12. }
    13. }
    14. //重写initView方法(其实就是在父类的onCreate()方法里面,不用在意)
    15. override fun initView() {
    16. //添加fragment
    17. fragments.add(FriendListFragment())//好友列表
    18. fragments.add(MessageListFragment())//聊天列表
    19. //为viewpager2设置adapter,重写getItemCount方法和createFragment方法,就把fragment添加到viewpager里面了
    20. viewPager.adapter = object : FragmentStateAdapter(this) {
    21. override fun getItemCount() = fragments.size
    22. override fun createFragment(position: Int) = fragments[position]
    23. }
    24. //为tablayout的tabItem添加标题,并且将tablayout和viewpager2绑定
    25. TabLayoutMediator(tabLayout, viewPager) { tab, position ->
    26. tab.text = titles[position]
    27. }.attach()
    28. }
    29. //重写initListener方法(其实就是在父类的onCreate()方法里面,不用在意)
    30. override fun initListener() {
    31. //为tablayout添加select监听器,这儿主要是实现选中时的标题字体大小、颜色、粗细等发生改变
    32. tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
    33. override fun onTabSelected(tab: TabLayout.Tab) {
    34. val customView = tab.customView
    35. if (customView == null) {
    36. tab.setCustomView(R.layout.tab_text_layout)
    37. }
    38. val textView: TextView? = tab.customView?.findViewById(android.R.id.text1)
    39. textView?.setTextAppearance(requireContext(), R.style.TabLayoutTextSelected)
    40. }
    41. override fun onTabUnselected(tab: TabLayout.Tab) {
    42. val customView = tab.customView
    43. if (customView == null) {
    44. tab.setCustomView(R.layout.tab_text_layout)
    45. }
    46. val textView: TextView? = tab.customView?.findViewById(android.R.id.text1)
    47. textView?.setTextAppearance(requireContext(), R.style.TabLayoutTextUnSelected)
    48. }
    49. override fun onTabReselected(tab: TabLayout.Tab) {}
    50. })
    51. }
    52. }

    3.tab选中和未选中时的文本

    1. <style name="TabLayoutTextSelected">
    2. <item name="android:textSize">24sp</item>
    3. <item name="android:textStyle">bold</item>
    4. <item name="android:textColor">@color/white</item>
    5. </style>
    6. <style name="TabLayoutTextUnSelected">
    7. <item name="android:textSize">14sp</item>
    8. <item name="android:textStyle">normal</item>
    9. <item name="android:textColor">@color/white_ccffffff</item>
    10. </style>

    4、实现效果

    三、常用属性介绍

    1. tabIndicatorFullWidth=false/true tab的indicator是否和文本一样宽
    2. tabIndicatorGravity  tab的indicator在tabitem的展示位置,顶部,居中,底部等等
    3. tabTextAppearance 文本的展示样式,有时候需求需要切换item后不止颜色改变,还需要改变大小,粗细等等,就可以自定义一个style文件,例如上面的例子是在代码里根据监听动态设置的,下面是在xml设置:
    1. //这是自定义的style文件
    2. <style name="TabLayoutTheme">
    3. <item name="android:textSize">16sp</item>
    4. <item name="android:textStyle">bold</item>
    5. </style>
    6. //这是xml布局文件,设置app:tabTextAppearance属性
    7. <com.google.android.material.tabs.TabLayout
    8. ...
    9. app:tabTextAppearance="@style/TabLayoutTheme"
    10. >

            4.tabSelectedTextColor 和 tabTextColor 字面意思

            5.app:tabIndicator="@drawable/tab_indicator" 设置自定义indicator

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:shape="rectangle">
    4. <item android:gravity="center">
    5. <shape>
    6. <size
    7. android:width="24dp"
    8. android:height="4dp" />
    9. <solid android:color="#FFF65F2A" />
    10. </shape>
    11. </item>
    12. </layer-list>

            6.app:tabIndicatorColor="#FFF65F2A" //indicator的颜色

            7.tabMode:tabMode属性用于设置tab是否可以横向滚动,可选的值有fixed(默认)、auto、scrollable。

            8.tabIndicatorHeight:这个属性设置指示器的高度,如果我们不需要显示指示器,则可以设置tabIndicatorHeight 为0

            

    参考文章:TabLayout的Indicator自定义宽度_文韬_武略的博客-CSDN博客 

    TabLayoutMediator  |  Android Developers

  • 相关阅读:
    Python算法题集_搜索旋转排序数组
    2023年上半年系统集成项目管理工程师什么时候报考?(附具体报名步骤)
    数据结构学习笔记(6)--特殊矩阵的压缩存储
    Flutter --- 配置多个Flutter SDK
    Java设计模式之状态模式
    C语言学生成绩信息管理系统【结构体+文本】
    【Linux学习】05-1Linux上安装部署各类软件
    管理类联考——数学——汇总篇——知识点突破——代数——等差数列——最值
    C++:类的六个默认成员函数
    Nacos
  • 原文地址:https://blog.csdn.net/LoveFHM/article/details/125401812