转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/78727255
本文出自【DylanAndroid的博客】


# Android中TabLayout添加小红点 Android中TabLayout添加小红点_第1张图片

1.主布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    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.support.design.widget.TabLayout        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" />LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="40dp"    android:layout_gravity="center"    android:orientation="horizontal">    <TextView        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" />    <TextView        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" />LinearLayout>

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) {            }        });    }}

更多相关文章

  1. Android帧布局实现霓虹灯的效果
  2. Android异步加载图片,并缓存到SD卡
  3. Android实现文件上传功能(接收端用strust2)
  4. android 获取文件的扩展名和去掉文件的扩展名
  5. android平台上的文件下载,文件和文件的操作
  6. Android百分比布局(PercentRelativeLayout)嵌套NavigationView自
  7. Android WebView加载HTML表单并通过javascript提交

随机推荐

  1. mysql蠕虫复制基础知识点
  2. Mysql数据表中的蠕虫复制使用方法
  3. MySQL limit性能分析与优化
  4. MySQL和Redis实现二级缓存的方法详解
  5. MySQL普通索引和唯一索引的深入讲解
  6. 使用MySQL的geometry类型处理经纬度距离
  7. mysql binlog(二进制日志)查看方法
  8. mysql导出表的字段和相关属性的步骤方法
  9. MySQL线程处于Opening tables的问题解决
  10. 解决Can&#39;t locate ExtUtils/MakeMake