国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-10-22 18:21:13
字體:
來源:轉載
供稿:網友

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

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("待發貨");    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) {      }    });  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 留坝县| 阿拉善左旗| 苏州市| 邻水| 商水县| 芦山县| 迁西县| 清涧县| 广平县| 惠东县| 永福县| 洪雅县| 延长县| 德安县| 分宜县| 昌邑市| 长垣县| 焦作市| 兴海县| 商河县| 胶州市| 万山特区| 长丰县| 北川| 大安市| 久治县| 巴马| 西藏| 平阴县| 宁南县| 台南县| 红桥区| 社会| 房产| 瑞昌市| 申扎县| 任丘市| 普格县| 双流县| 筠连县| 偃师市|