tablayout 增加数字小标_为 TabLayout 添加角标的最简单方法

一、前言

在开发中,我们常常需要 ViewPager 结合 Fragment一起使用,来实现多页签的切换效果。在以前,我们有以下一系列第三方库来帮我们实现:

而现在,我们可以使用Design support library 库的 TabLayout 来实现了。

二、最终效果图

三、TabLayout 的使用

1. 添加依赖

由于 TabLayout 在 design 包内,所以首先需要在 app 目录下的 build.gradle 中添加以下依赖:

1

2

3

4dependencies {

...

compile 'com.android.support:design:23.4.0'

}

2. 创建布局

布局相当简单,只要添加 TabLayout 和 ViewPager 的布局即可:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23// layout/activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/tab_layout"

style="@style/TabLayoutStyle"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/view_pager"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:background="@android:color/white"/>

还有其他的属性我习惯在style文件中设置:

1

2

3

4

5

6

7

8

9

10

11

12

13// values/styles.xml

@color/colorPrimary

@color/colorPrimary

@style/TabTextAppearence

0dp

16sp

false

3. 创建 Fragment1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38package com.sherlockshi.badgedtablayoutpractise;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;<

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值