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

一、前言

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

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

二、最终效果图

c8ea998a982a?nomobile=yes

三、TabLayout的使用

1. 添加依赖

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

dependencies {

...

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

}

2. 创建布局

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

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文件中设置:

values/styles.xml

@color/colorPrimary

@color/colorPrimary

@style/TabTextAppearence

0dp

16sp

false

3. 创建Fragment

package 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;

import android.view.ViewGroup;

import android.widget.TextView;

/**

* Author: SherlockShi

* Date: 2016-11-01 16:31

* Description:

*/

public class

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值