2017-10-17 192 views
0

現在我只有一個簡單的標籤佈局與查看傳呼機一起工作。我想知道如何自定義我的標籤佈局,以便例如在我的應用程序中顯示一個功能,以顯示我的查看傳呼機中該片段的回收器視圖中的項目數。 感謝如何在我的標籤佈局中像Whatsapp一樣製作計數器?

This is an example

+0

你可以在標籤標題中使用跨度嗎?如果是這樣,可能會在某個地方開始。 – stkent

回答

1
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; 
} 
0

你可以做這樣的事情:

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); 

這就是它!

相關問題