溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中使用TabLayout添加小紅點(diǎn)

發(fā)布時(shí)間:2021-04-12 16:06:10 來(lái)源:億速云 閱讀:468 作者:Leah 欄目:移動(dòng)開發(fā)

今天就跟大家聊聊有關(guān)怎么在Android中使用TabLayout添加小紅點(diǎn),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

<?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.要顯示小紅點(diǎn)的自定義布局文件

<?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.設(shè)置TabLayout加載的各個(gè)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);
    //待付款欄目-加載自定義顯示小紅點(diǎn)的布局
    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("待發(fā)貨");
    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選中監(jiān)聽
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
        //設(shè)置選中時(shí)的文字顏色
        if (tab.getCustomView() != null) {
          tv_tab_title.setTextColor(getResources().getColor(R.color.colorAccent));
        }
      }

      @Override
      public void onTabUnselected(TabLayout.Tab tab) {
        //設(shè)置未選中時(shí)的文字顏色
        if (tab.getCustomView() != null) {
          tv_tab_title.setTextColor(getResources().getColor(R.color.colorBlack));
        }
      }

      @Override
      public void onTabReselected(TabLayout.Tab tab) {
      }
    });
  }
}

看完上述內(nèi)容,你們對(duì)怎么在Android中使用TabLayout添加小紅點(diǎn)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI