TabLayout-简单使用

本文详细介绍了Android中TabLayout的使用,包括如何在XML和代码中添加Tab,以及TabLayout的重要属性设置,如指示器颜色、宽度、文字颜色等。此外,还展示了如何实现图文混排,通过自定义View在Tab中添加图片,提供了具体代码示例和效果展示。
摘要由CSDN通过智能技术生成

TabLayout

一:简介

TabLayout提供了一个水平布局用于展示tabs,继承自HorizontalScrollView。一般与Viewpager结合使用实现页面和标签联动的效果,是时下APP中非常常用的一个控件

二:基本用法

  1. xml中添加tab:
<com.google.android.material.tabs.TabItem  //在XML中添加item的控件,此控件是TabLayout的子控件
<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item1" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item2" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item3" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item4" />

    </com.google.android.material.tabs.TabLayout>

相当于给TabLayout添加了四个item,即
在这里插入图片描述
此时你的模拟器上的TabLayout拥有了四个指示标记。
2. 还可在代码中添加tab

		tableLayout = findViewById(R.id.tabLayout);
        tableLayout.addTab(tableLayout.newTab().setText("Tab0"));
        tableLayout.addTab(tableLayout.newTab().setText("Tab1"));
        tableLayout.addTab(tableLayout.newTab().setText("Tab2"));
        tableLayout.addTab(tableLayout.newTab().setText("Tab3"));
        tableLayout.addTab(tableLayout.newTab().setText("Tab4"));

先获取TabLayout实例,然后通过调用方法添加Tab,Tab是TabLayout的内部类

三. 属性详解

<declare-styleable name="TabLayout">
    <!--指示器颜色-->
    <attr name="tabIndicatorColor" format="color"/>
    <!--指示器高度-->
    <attr name="tabIndicatorHeight" format="dimension"/>
     <!--指示器宽度 true:和tab同宽  false:和tab中的字同宽 -->
    <attr name="tabIndicatorFullWidth" format="boolean"/>
    <!--tabs距TabLayout开始位置的偏移量,但app:tabMode="scrollable"才生效-->
    <attr name="tabContentStart" format="dimension"/>
    <!--仅是Tab背景,设置TabLayout背景用android:background-->
    <attr name="tabBackground" format="reference"/>
    <!--默认fixed,所有Tab只能在屏幕内显示,超出会被挤压;scrollable,tab数量多会超出屏幕,可滑动-->
    <attr name="tabMode">
        <enum name="scrollable" value="0"/>
        <enum name="fixed" value="1"/>
    </attr>
    <!--默认fill,tab填满TabLayout,但tabMode=“fixed”才生效;center,tabs位于TabLayout的中间-->
    <attr name="tabGravity">
        <enum name="fill" value="0"/>
        <enum name="center" value="1"/>
    </attr>
    <!--Tab的最小宽度-->
    <attr name="tabMinWidth" format="dimension"/>
    <!--Tab的最大宽度-->
    <attr name="tabMaxWidth" format="dimension"/>
    <!--Tab文本设置样式-->
    <attr name="tabTextAppearance" format="reference"/>
    <!--Tab未选中字体颜色-->
    <attr name="tabTextColor" format="color"/>
    <!--Tab选中字体颜色-->
    <attr name="tabSelectedTextColor" format="color"/>
    <!--Tab内填充相关-->
    <attr name="tabPaddingStart" format="dimension"/>
    <attr name="tabPaddingTop" format="dimension"/>
    <attr name="tabPaddingEnd" format="dimension"/>
    <attr name="tabPaddingBottom" format="dimension"/>
    <attr name="tabPadding" format="dimension"/>
</declare-styleable>

四,图文混排,Tab中添加图片

  1. 通过Tab.setCustomView(View view) 设置图片
    1. 自定义VIew布局
      此布局名为tabview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="16sp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:text="首页"
        android:textSize="16sp" />

</LinearLayout>
  1. 代码设置:
	//自定义方法用于VIew对象的创建,加载自己的布局
	private View setCustomView(int drawable, String tabText) {		
        View view = View.inflate(this, R.layout.tabview ,null);		//动态加载布局
        ImageView iv = (ImageView) view.findViewById(R.id.iv);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        iv.setImageResource(drawable);
        tv.setText(tabText);
        return view;
    }
	
	 new TabLayoutMediator(findViewById(R.id.tabLayout), viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setCustomView(setCustomView(R.drawable.ic_baseline_home_24,"item" + position));		//此处将自己的view对象加载给tab,使其显示出来
            }
        }).attach();

效果展示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值