最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中TabLayout添加小紅點(diǎn)的示例代碼

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

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

 

安卓原生的android.support.design.widget.TabLayout,配合ViewPager已經(jīng)很好用了,但是有時(shí)我們會(huì)在內(nèi)容更新時(shí),在tab標(biāo)題右上方加上一個(gè)紅點(diǎn)等標(biāo)記此tab內(nèi)容有更新時(shí),就需要給原生的TabLayout設(shè)置你定義的布局,用法和原生的一樣,只是在代碼中設(shè)置一下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.要顯示小紅點(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)聽(tīng)
    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) {
      }
    });
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

西乌珠穆沁旗| 高邑县| 包头市| 陆河县| 开平市| 上饶县| 麻城市| 安远县| 尚义县| 河源市| 永福县| 靖安县| 冀州市| 塔河县| 全椒县| 六安市| 长垣县| 毕节市| 洛浦县| 封丘县| 绥江县| 剑阁县| 重庆市| 九龙坡区| 手游| 彭山县| 特克斯县| 双城市| 罗甸县| 纳雍县| 三台县| 石首市| 洛隆县| 荥阳市| 合水县| 姜堰市| 灵宝市| 潍坊市| 比如县| 新乡市| 定安县|