如何像 Whatsapp 一样在我的选项卡布局中创建一个计数器?

How to make a counter inside my tab layout like Whatsapp?

现在我只有一个简单的选项卡布局与视图寻呼机一起使用。我想知道如何自定义我的选项卡布局,以便在我的应用程序中有一个功能,例如在我的视图寻呼机中显示我的回收站视图中该片段的项目数。 谢谢

你可以这样做:

LinearLaout tabone= (LinearLaout ) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);

TextView txtView= (TextView) tabone.findViewById(R.id.txtview);
txtView.setText("ONE");

TextView txtCount= (ImageView ) tabone.findViewById(R.id.txtcount);
txtCount.setText("200")//Count
/*Also don't forgot to add background of count textview in xml according to your design.*/

 tabLayout.getTabAt(0).setCustomView(tabone);

就是这样!!

可以参考

https://github.com/rahimlis/badgedtablayout?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=6087

https://github.com/Devlight/NavigationTabBar

https://github.com/matrixxun/MaterialBadgeTextView?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=4670

希望对您的问题有所帮助!

public View getTabView(Context context, int position) {
    int[] unreadCount = {3, 1, 2};

    View tab = LayoutInflater.from(context).inflate(R.layout.partial_custom_tab, null);
    tabText = (TextView) tab.findViewById(R.id.tab_title);
    counter = (View) tab.findViewById(R.id.tab_badge);
    tabText.setText(mPages.get(position).title);
    if (mPages.get(position).icon != null) {
        counter.setVisibility(View.VISIBLE);
        counter.setBackground(mPages.get(position).icon);
    } else {
        counter.setVisibility(View.GONE);
    }

    BadgeDrawable badge = new BadgeDrawable(getContext(),
            getResources().getColor(R.color.colorAccent));

    if (unreadCount[position] > 0) {
        counter.setVisibility(View.VISIBLE);
        badge.setCount(unreadCount[position]);
        badge.mutate();
        counter.setBackground(badge);
    } else {
        counter.setVisibility(View.GONE);
    }

    if (position == 0) {
        tab.setSelected(true);
    }
    return tab;
}