欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中TabLayout添加小紅點的示例代碼

 更新時間:2017年12月06日 10:14:43   作者:DylanAndroid  
本篇文章主要介紹了Android中TabLayout添加小紅點的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了Android中TabLayout添加小紅點的示例代碼,分享給大家,具體如下

 

安卓原生的android.support.design.widget.TabLayout,配合ViewPager已經很好用了,但是有時我們會在內容更新時,在tab標題右上方加上一個紅點等標記此tab內容有更新時,就需要給原生的TabLayout設置你定義的布局,用法和原生的一樣,只是在代碼中設置一下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("待發(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) {
        //設置選中時的文字顏色
        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中Hilt的使用詳解

    Android中Hilt的使用詳解

    Hilt?是?Android?的依賴項注入庫,可減少在項目中執(zhí)行手動依賴項注入的樣板代碼,本文就來為大家介紹一下Hilt的具體使用吧,希望對大家有所幫助
    2023-06-06
  • Android框架RePlugin使用詳解

    Android框架RePlugin使用詳解

    這篇文章給大家分享了Android 插件化框架 RePlugin使用心得,對此有興趣的朋友參考學習下。
    2018-07-07
  • Flutter繪圖組件之CustomPaint使用詳解

    Flutter繪圖組件之CustomPaint使用詳解

    CustomPaint是Flutter中用于自由繪制的一個widget,它與android原生的繪制規(guī)則基本一致,以當前Canves(畫布)的左上角為原點進行繪制。本文將詳細講解CustomPaint的使用教程,需要的可以參考一下
    2022-03-03
  • Android中Glide庫的使用小技巧總結

    Android中Glide庫的使用小技巧總結

    Glide是 Google推薦的圖片加載庫,相信大家都不陌生,這篇文章主要給大家總結介紹了關于Android中Glide庫的使用小技巧,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-11-11
  • 基于fluttertoast實現(xiàn)封裝彈框提示工具類

    基于fluttertoast實現(xiàn)封裝彈框提示工具類

    這篇文章主要為大家介紹了基于fluttertoast實現(xiàn)封裝彈框提示工具類的實現(xiàn)代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • zxing二維碼位矩陣轉換成Bitmap位圖的實戰(zhàn)教程

    zxing二維碼位矩陣轉換成Bitmap位圖的實戰(zhàn)教程

    二維碼的應用已經可以說是非常廣泛了,下面這篇文章主要給大家介紹了關于zxing二維碼位矩陣轉換成Bitmap位圖的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • android實現(xiàn)文件下載功能

    android實現(xiàn)文件下載功能

    這篇文章主要為大家詳細介紹了android實現(xiàn)文件下載功能,android在網絡上下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android仿音樂播放器功能

    Android仿音樂播放器功能

    這篇文章主要為大家詳細介紹了Android仿音樂播放器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android實現(xiàn)圓角矩形和圓形ImageView的方式

    Android實現(xiàn)圓角矩形和圓形ImageView的方式

    這篇文章主要為大家詳細介紹了Android中實現(xiàn)圓角矩形和圓形的方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 一文帶你深入理解Android Window系統(tǒng)

    一文帶你深入理解Android Window系統(tǒng)

    Android中的窗口系統(tǒng)是應用程序用戶界面的核心組件之一,它負責管理可視化區(qū)域、處理用戶輸入事件以及與系統(tǒng)UI交互,本文將深入介紹與Android窗口系統(tǒng)相關的重要概念,需要的朋友可以參考下
    2023-10-10

最新評論