TabLayout使用指南

TabLayout是开发中经常使用到的控件,经常与ViewPager一起配合使用,一组tab,可以点击、可以滚动。这不,我们的app中也是用到了这个控件,之前对这个控件只停留在最基本的用法,因此开发时也去查了些资料,趁着周末,就系统地再学习一下。

基本操作

使用之前,首先需要在gradle文件中加入design库,

    implementation 'com.android.support:design:28.0.0'

首先看一下最默认的行为与效果。 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp">

    </android.support.design.widget.TabLayout>

</android.support.constraint.ConstraintLayout>
tabLayout.addTab(tabLayout.newTab().setText("Tab 1").setIcon(R.mipmap.ic_launcher));
        tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
        tabLayout.addTab(tabLayout.newTab().setText("Tab 3").setIcon(R.mipmap.ic_launcher));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Toast.makeText(MainActivity.this,tab.getText(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Toast.makeText(MainActivity.this,tab.getText(),Toast.LENGTH_SHORT).show();
            }
        });

setOnTabSelectedListener是一个过时方法,官方建议使用addOnTabSelectedListener代替,其中监听器接口是一样的,分别表示tab选中、未选中,再次选中状态。其中再次选中状态可以用于实现在选中tab的前提下,再点击时,滚动到最顶部的效果,比如很多资讯类app就是这么实现的。

效果图如下:

image

默认效果可以看到指示器红色,三个tab平分布局,有icon的显示在文字上方。 如果TabLayout的宽度wrap_content,那么三个tab将会挤到左边,每个tab的效果是wrap_content。

以上tab是通过代码添加的,也可以在xml中进行添加,效果等效于

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp">

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:icon="@mipmap/ic_launcher"
            android:text="Tab 1" />

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Tab 2" />

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:icon="@mipmap/ic_launcher"
            android:text="Tab 3" />

    </android.support.design.widget.TabLayout>

</android.support.constraint.ConstraintLayout>

xml属性说明

对于TabLayout的样式修改,一些可以通过修改属性就行修改。

修改指示器

可以修改指示器的颜色和高度,比如:

<android.support.design.widget.TabLayout
        app:tabIndicatorColor="@android:color/holo_green_dark"
        app:tabIndicatorHeight="10dp"
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp"/>

效果如下:

image

修改文字样式

  1. 选中和未选中颜色设置
app:tabSelectedTextColor="#FF0000"
        app:tabTextColor="#0000FF"

效果如下图:

image

  1. 修改文字大小
    使用textAppearence属性,在style文件中设置大小。
        app:tabTextAppearance="@style/my_tab_text_style"

my_tab_text_style内容如下:

<style name="my_tab_text_style">
        <item name="android:textSize">20sp</item>
        <item name="android:textStyle">bold</item>
    </style>

效果如下:


image

修改tab样式

  1. 使用padding参数,可以使用tabPadding进行设置,比如:

    <android.support.design.widget.TabLayout
            app:tabPaddingTop="20dp"
            android:paddingBottom="20dp"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    

    可以看到预览图的效果如下:

    image
  2. tabMode。tabMode支持两种值,MODE_FIXED和MODE_SCROLLABLE;当tab比较多,一屏容纳不下时,会使用MODE_SCROLLABLE,这时可以隐藏部分MODE;而FIXED的就会始终显示。

    当在xml布局中添加了很多TabItem后,预览效果如下图:

    image

    这时使用的就是FIXED模式,可以看到TabLayout默认就是FIXED模式;当改成MODE_SCROLLABLE后,

            app:tabMode="scrollable"
    

    预览样式如下图:

    image
  3. Tab位置。当只有三个tab时,默认分散了,如果想三个tab聚合起来,可以通过设置tabGravity属性进行设置,比如:

            app:tabGravity="center"
    

    设置后的效果如下:

    image

    设置前的效果就是前面三个tab平铺的效果。改属性的默认值是fill。

    另外,可以通过设置tabContentStart设置偏移量,类似margin。

    比如设置:

            app:tabContentStart="100dp"
    

    后的效果如下图:

    image
  4. 设置tab背景。tabBakcground属性,比如设置的样式如下:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
    
        <size
            android:width="50dp"
            android:height="30dp"
            ></size>
    
        <gradient
            android:startColor="#FF0000"
            android:endColor="#0000FF"
            ></gradient>
    
    </shape>
    

    效果图:

    image
  5. tab宽度,可以通过设置tabMaxWidth和tabMinWidth进行限制。

TabItem样式自定义

以上的xml样式,都可以通过相应的set方法进行设置,但是如果想改变默认的tab样式,那么就需要代码的操作了。默认的tab样式,icon在上,text在下;下面改个icon在左,text在右的样式。

首先定义一个布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="48dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:padding="12dp"
        android:id="@+id/iv_icon"
        tools:src="@mipmap/ic_launcher"
        android:layout_width="48dp"
        android:layout_height="48dp" />

    <TextView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:textSize="18sp"
        android:textColor="#333333"
        android:id="@+id/tv_text"
        android:layout_width="0dp"
        android:layout_height="21dp"
        app:layout_constraintLeft_toRightOf="@id/iv_icon"
        app:layout_constraintRight_toRightOf="parent"
        tools:text="hha" />


</android.support.constraint.ConstraintLayout>

然后在代码中更改TabLayout现有Tab样式,代码如下:

tabLayout=findViewById(R.id.tabLayout);

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            View view=LayoutInflater.from(this).inflate(R.layout.my_tab_layout,null);
            tab.setCustomView(view);
            ((ImageView)view.findViewById(R.id.iv_icon)).setImageDrawable(tab.getIcon());
            ((TextView)view.findViewById(R.id.tv_text)).setText(tab.getText());
        }

运行结果如下图:

image

可以看到tab样式已经变成了icon在左,文字在右的样式。

集成ViewPager

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp">
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        app:layout_constraintTop_toBottomOf="@id/tabLayout"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:layout_height="0dp"></android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

代码如下:

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private int[] imgs=new int[]{
            R.drawable.pic_11,
            R.drawable.pic_12,
            R.drawable.pic_11
    };

    private String[] tabs=new String[]{
      "Tab 1",
      "Tab 2",
      "Tab 3",
    };


    private List<ImgFragment> imgFragments=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout=findViewById(R.id.tabLayout);
        viewPager=findViewById(R.id.viewpager);

        for (int i = 0; i < imgs.length; i++) {
            imgFragments.add(ImgFragment.newInstance(imgs[i]));
        }

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return imgFragments.get(i);
            }

            @Override
            public int getCount() {
                return imgFragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });

        tabLayout.setupWithViewPager(viewPager);

    }
}

运行效果如下:

image

这里需要注意的是:当调用了setupWithViewPager之后,tab值默认将会从getPageTitle中获取;如果这个时候没有重写PageAdapter的getPageTitle,那么效果将会如下图:
image

这个时候可以发现TabLayout上面都是空的,但其实是有tab的,只不过tab内容为空而已。这个时候可以通过代码重新设置,比如:

tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab=tabLayout.getTabAt(i);
            tab.setText(tabs[i]);
            tab.setIcon(imgs[i]);
        }

效果如下图:

image

可以看到,不止设置了text,还设置了title,使用PageAdapter只能设置text。

总结

至此,TabLayout的基本用法也就是这样了;除了这个,还有与Toolbar以及协调布局共同使用的情况,这个以后有机会会继续深入的学习下。

DEMO地址

参考内容

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容