如何在Tablayout中显示通知计数器? [英] How to show Notification Counter in a Tablayout?

查看:134
本文介绍了如何在Tablayout中显示通知计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了此堆栈溢出信息但这并没有帮助我理解我必须做的事情.我了解如何从查询中增加计数器的值的概念.

I saw this stack overflow post but it didn't help me understand what I had to do. I understand the concept of how I am going to increment the counter's value from my query.

但是我不明白我应该使用什么.我应该使用 Notification notification; 还是 BadgeViewer badgeViewer; ?我希望我不必导入库.

But I don't understand what I should use. Should I use Notification notification; or BadgeViewer badgeViewer;? I would prefer a way where I don't have to import a library.

推荐答案

为选项卡添加布局资源.它应该包含代表您的徽章"的内容(如果您只想显示一个数字,一个简单的 TextView 即可使用).创建标签时,请指定此自定义布局.

Make a layout resource for your tabs. It should include something that represents your "badge" (a simple TextView will work if you just want to show a number). When you create the tabs, specify this custom layout.

private TabLayout tabLayout;

// in onCreate()
tabLayout = (TabLayout) findViewById(R.id.tabs);
for (int i = 0; i < NUM_TABS; i++) {
    TabLayout.Tab tab = tabLayout.newTab()
            .setText("tab name")
            .setCustomView(R.layout.custom_tab);
    tabLayout.addTab(tab);
}

请注意,为使 setText()正常工作,您的布局需要带有 android:id ="@ android:id/text1" TextView 代码>(这不是徽章文本,这是标签名称).

Note that for setText() to work properly, your layout needs a TextView with android:id="@android:id/text1" (this is not the badge text, this is the tab name).

要更新标签的徽标时,可以获取 Tab 并询问其视图,找到徽标并设置其文本.

When you want to update the badge for a tab, you can get the Tab and ask for its view, find the badge, and set its text.

TabLayout.Tab tab = tabLayout.getTabAt(3); // fourth tab
View tabView = tab.getCustomView();
TextView badgeText = (TextView) tabView.findViewById(R.id.badge_text);
badgeText.setText(...);

这篇关于如何在Tablayout中显示通知计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆