Android中TabLayout添加小红点

Android中TabLayout添加小红点,第1张

概述# Android中TabLayout添加小红点1.主布局文件<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.c...

# AndroID中TabLayout添加小红点

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

 

总结

以上是内存溢出为你收集整理的Android中TabLayout添加小红点全部内容,希望文章能够帮你解决Android中TabLayout添加小红点所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存