Android中TabLayout添加小紅點(diǎn)的示例代碼
本文介紹了Android中TabLayout添加小紅點(diǎn)的示例代碼,分享給大家,具體如下
安卓原生的android.support.design.widget.TabLayout,配合ViewPager已經(jīng)很好用了,但是有時我們會在內(nèi)容更新時,在tab標(biāo)題右上方加上一個紅點(diǎn)等標(biāo)記此tab內(nèi)容有更新時,就需要給原生的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加載的各個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è)置選中時的文字顏色
if (tab.getCustomView() != null) {
tv_tab_title.setTextColor(getResources().getColor(R.color.colorAccent));
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//設(shè)置未選中時的文字顏色
if (tab.getCustomView() != null) {
tv_tab_title.setTextColor(getResources().getColor(R.color.colorBlack));
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)消息提醒小紅點(diǎn)效果
- Android應(yīng)用圖標(biāo)上的小紅點(diǎn)Badge實(shí)踐代碼
- Android高仿QQ小紅點(diǎn)功能
- Android仿QQ未讀消息--紅點(diǎn)拖拽刪除【源代碼】
- Android 未讀消息的紅點(diǎn)顯示
- Android帶數(shù)字或紅點(diǎn)的底部導(dǎo)航攔和聯(lián)網(wǎng)等待加載動畫示例
- Android BadgeView紅點(diǎn)更新信息提示示例代碼
- Android開發(fā)中TextView 實(shí)現(xiàn)右上角跟隨文本動態(tài)追加圓形紅點(diǎn)
- Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn)
- Android 基于MediatorLiveData實(shí)現(xiàn)紅點(diǎn)的統(tǒng)一管理
相關(guān)文章
Android獲取觸摸手勢實(shí)現(xiàn)左右滑動
這篇文章主要為大家詳細(xì)介紹了Android獲取觸摸手勢實(shí)現(xiàn)左右滑動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
詳解Flutter如何在單個屏幕上實(shí)現(xiàn)多個列表
這篇文章主要為大家詳細(xì)介紹了Flutter如何在單個屏幕上實(shí)現(xiàn)多個列表,這些列表可以水平排列、網(wǎng)格格式、垂直排列,甚至是這些常用布局的組合,感興趣的小伙伴可以了解下2023-11-11
kotlin 官方學(xué)習(xí)教程之基礎(chǔ)語法詳解
這篇文章主要介紹了kotlin 官方學(xué)習(xí)教程之基礎(chǔ)語法詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android 再按一次退出程序的實(shí)現(xiàn)方法
抽空研究了一下"再按一次退出程序"的實(shí)現(xiàn)方式,直接上代碼(請自動忽略無關(guān)代碼)2014-01-01
Android來電監(jiān)聽和去電監(jiān)聽實(shí)現(xiàn)代碼
本文是關(guān)于來點(diǎn)監(jiān)聽和去電監(jiān)聽展開問題,通過實(shí)例代碼講解,對android來電監(jiān)聽和去電監(jiān)聽的相關(guān)知識感興趣的朋友一起看看吧2017-06-06
Android自定義相機(jī)實(shí)現(xiàn)定時拍照功能
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)實(shí)現(xiàn)定時拍照功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
android重力感應(yīng)開發(fā)之微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了android重力感應(yīng)開發(fā)之微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05

