tablayout 增加数字小标_Android中TabLayout添加小红点

本文介绍如何在Android应用中为TabLayout增加数字小标,展示了一个实例,涉及自定义布局文件、TabLayout的使用以及设置Tab的内容和选中状态时的样式变化。
摘要由CSDN通过智能技术生成

Android中TabLayout添加小红点

[图片上传失败...(image-fefdfa-1512524938651)]

1.主布局文件

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

android:layout_width="match_parent"

android:layout_height="match_parent"

xmlns:app="http://schemas.android.com/apk/res-auto"

tools:context="com.bxkj.dylan.tablayoutreddot.MainActivity">

android:id="@+id/tabLayout"

app:tabBackground="@android:color/white"

app:tabTextColor="@color/colorBlack"

app:tabSelectedTextColor="@color/colorAccent"

app:tabMode="scrollable"

android:layout_width="match_parent"

android:layout_height="40dp" />

2.要显示小红点的自定义布局文件

android:layout_width="wrap_content"

android:layout_height="40dp"

android:layout_gravity="center"

android:orientation="horizontal">

android:id="@+id/tv_tab_title"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:textColor="@color/colorBlack"

android:textSize="15sp" />

android:id="@+id/iv_tab_red"

android:layout_gravity="right"

android:layout_width="18dp"

android:text="5"

android:gravity="center"

android:textColor="@android:color/white"

android:layout_height="18dp"

android:background="@drawable/red_dot" />

3.设置TabLayout加载的各个Tab

import android.content.res.Resources;

import android.support.design.widget.TabLayout;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

/**

* @author dylan

*/

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;

private TextView tv_tab_title;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tabLayout = findViewById(R.id.tabLayout);

initData();

}

private void initData() {

TabLayout.Tab tab = tabLayout.newTab().setText("全部");

tabLayout.addTab(tab);

//待付款栏目-加载自定义显示小红点的布局

tab = tabLayout.newTab();

tab.setCustomView(R.layout.tab_wait_for_pay);

tv_tab_title = tab.getCustomView().findViewById(R.id.tv_tab_title);

tv_tab_title.setText("待付款");

tabLayout.addTab(tab);

tab = tabLayout.newTab().setText("待发货");

tabLayout.addTab(tab);

tab = tabLayout.newTab().setText("待收货");

tabLayout.addTab(tab);

tab = tabLayout.newTab().setText("已完成");

tabLayout.addTab(tab);

tab = tabLayout.newTab().setText("已取消");

tabLayout.addTab(tab);

//添加tabLayout选中监听

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override

public void onTabSelected(TabLayout.Tab tab) {

//设置选中时的文字颜色

if (tab.getCustomView() != null) {

tv_tab_title.setTextColor(getResources().getColor(R.color.colorAccent));

}

}

@Override

public void onTabUnselected(TabLayout.Tab tab) {

//设置未选中时的文字颜色

if (tab.getCustomView() != null) {

tv_tab_title.setTextColor(getResources().getColor(R.color.colorBlack));

}

}

@Override

public void onTabReselected(TabLayout.Tab tab) {

}

});

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值