Android TabLayout选项卡使用教程

 更新时间:2023年04月06日 09:49:34   作者:别偷我的猪_09  
这篇文章主要介绍了Android TabLayout选项卡使用,为什么会有这篇文章呢,是因为之前关于TabLayout的使用陆陆续续也写了好几篇了,感觉比较分散,且不成体系,写这篇文章的目的就是希望能把各种效果的实现一次性讲齐

TabLayout

TabLayout 在开发中一般作为选项卡使用,常与 ViewPager2 和Fragment 结合起来使用。

常用属性:

app:tabBackground 设置 TabLayout 的背景色,改变整个TabLayout 的颜色;

app:tabTextColor 设置未被选中时文字的颜色;

app:tabSelectorColor 设置选中时文字颜色;

app:tabTextAppearance="@android:style/TextAppearance.Large" 设置 TabLayout 的文本主题,无法通过 textSize 来设置文字大小,只能通过主题来设定;

app:tabMode="scrollable"设置 TabLayout 可滑动,当 tabItem 个数较多时,一个界面无法呈现所有的导航标签,此时就必须要用;

app:tabIndicator 设置指示器;

app:tabIndicatorColor 设置指示器颜色;

​​​​​​​ app:tabIndecatorHeight 设置指示器高度,当app:tabIndecatorHeight="0dp",隐藏 Indicator 效果;

app:tabTextAppearance="@android:style/TextAppearance.Holo.Large" 改变 TabLayout 里 TabItem 文字的大小;

app: tabPadding 设置 Tab 内部 item 的 padding。也可以单独设置某个方向的padding, 比如 app:tabPaddingStart 设置左边距;

app:paddingEdng / app:paddingStart 设置整个 TabLayout 的 padding;

app:tabGravity="center" 居中,如果是 fill,则充满;

app:tabMaxWidth / app:tabMinWidth 设置最大/最小的 tab 宽度,对 Tab 的宽度进行限制。

TabItem

给TabLayout 添加 Item 有两种方法,其中一种就是使用 TabItem 在 xml 里直接添加。

1. 使用TabItem 给 TabLayout 添加卡片。

<com.google.android.material.tabs.TabItem
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:icon="@android:drawable/ic_menu_add"
     android:text="添加"/>

android:icon 设置图标;

Android:text 设置文本;

2. 通过代码添加。使用 TabLayoutMediator()

        new TabLayoutMediator(binding.tab, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //TODO 设置卡片的文本/图标
                tab.setText(mTitles.get(position))
                   .setIcon(mIcons.get(position));
            }
        }).attach();

其中 mTitles 和 mIcons 是存放 text 和 Icon 的list。效果如下:

可以看到 text 在英文状态下默认都是大写,这是因为在 TabLayout 的源码中默认设置属性 textAllCaps=true。所以可以在 TabLayout 中设置如下属性来改成小写。

app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"

演示效果的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_add"
            android:text="添加"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_delete"
            android:text="删除"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_camera"
            android:text="相机"/>
    </com.google.android.material.tabs.TabLayout>
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs1"
        style="@style/Widget.MaterialComponents.TabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        android:layout_margin="8dp">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_add"
            android:text="添加"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_delete"
            android:text="删除"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_camera"
            android:text="相机"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_add"
            android:text="添加"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_delete"
            android:text="删除"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_camera"
            android:text="相机"/>
    </com.google.android.material.tabs.TabLayout>
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs2"
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@color/purple_700"
        android:layout_margin="8dp">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_add"
            android:text="添加"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_delete"
            android:text="删除"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_camera"
            android:text="相机"/>
    </com.google.android.material.tabs.TabLayout>
    <com.google.android.material.tabs.TabLayout
        android:layout_margin="8dp"
        android:id="@+id/tabs3"
        style="@style/Widget.Design.TabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_add"
            android:text="添加" />
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_call"
            android:text="删除" />
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_camera"
            android:text="菜单" />
    </com.google.android.material.tabs.TabLayout>
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs4"
        app:tabTextAppearance="@android:style/TextAppearance.Holo.Large"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorHeight="0dp"
        android:layout_margin="8dp">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_add"
            android:text="add"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_delete"
            android:text="删除"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/ic_menu_camera"
            android:text="相机"/>
    </com.google.android.material.tabs.TabLayout>
</LinearLayout>

到此这篇关于Android TabLayout选项卡使用教程的文章就介绍到这了,更多相关Android TabLayout内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android仿微信聊天图片的放大缩小功能

    Android仿微信聊天图片的放大缩小功能

    本文介绍了如何实现Android仿微信聊天图片的放大缩小效果,通过修改Android官方代码,实现点击图片放大,再次点击缩小回原位的功能,感兴趣的朋友跟随小编一起看看吧
    2025-02-02
  • Android ListView列表实现倒计时

    Android ListView列表实现倒计时

    这篇文章主要为大家详细介绍了Android ListView列表实现倒计时,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • android studio实验: UI设计 ListView及事件响应

    android studio实验: UI设计 ListView及事件响应

    这篇文章主要介绍了android studio实验: UI设计 ListView及事件响应,主要是ListView及其事件响应方法 弹出菜单PopupMenu及其事件响应方法,下面来看看具文章体的介绍吧
    2021-12-12
  • 微信多图上传解决android多图上传失败问题

    微信多图上传解决android多图上传失败问题

    这篇文章主要介绍了微信多图上传解决android多图上传失败问题,需要的朋友可以参考下
    2017-04-04
  • Android沉浸式状态栏实现

    Android沉浸式状态栏实现

    这篇文章主要介绍了Android沉浸式状态栏实现,即一体化状态栏实现,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android使用Retrofit仿微信多张图片拍照上传

    Android使用Retrofit仿微信多张图片拍照上传

    这篇文章主要介绍了Android使用Retrofit仿微信多张图片拍照上传的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android 后台发送邮件示例 (收集应用异常信息+Demo代码)

    Android 后台发送邮件示例 (收集应用异常信息+Demo代码)

    今天介绍个更简单的方法,我们把异常信息收集后,通过后台发送邮件方法,把相关异常信息发送到我们指定的邮箱里面
    2013-07-07
  • Flutter布局模型之层叠定位

    Flutter布局模型之层叠定位

    这篇文章主要为大家详细介绍了Flutter布局模型之层叠定位,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android 滑动小圆点ViewPager的两种设置方法详解流程

    Android 滑动小圆点ViewPager的两种设置方法详解流程

    Viewpager,视图翻页工具,提供了多页面切换的效果。Android 3.0后引入的一个UI控件,位于v4包中。低版本使用需要导入v4包,现在我们一般不再兼容3.0及以下版本,另外使用Android studio开发,默认导入v7包,v7包含了v4,所以不用导包,越来越方便了
    2021-11-11
  • Handler与Android多线程详解

    Handler与Android多线程详解

    一开始,相信很多人都以为myThread中的run()方法会在一个新的线程中运行,但事实并非如此。以下代码中的handler并没有调用线程myThread的start()方法,而是直接调用了run()方法,这也就意味着实际上并没有创建一个新的线程,只是在当前线程中调用run()方法而已
    2013-10-10

最新评论