怎么在Android中使用TabLayout添加小红点

今天就跟大家聊聊有关怎么在Android中使用TabLayout添加小红点,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了松阳免费建站欢迎大家使用!

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

看完上述内容,你们对怎么在Android中使用TabLayout添加小红点有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。

网站题目:怎么在Android中使用TabLayout添加小红点
浏览地址:https://www.cdcxhl.com/article44/gipghe.html

成都网站建设公司_创新互联,为您提供App设计外贸建站定制开发静态网站网站排名电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管