2014-09-29 92 views
6

我是徽章概念的新手。在我的应用程序中,我想在一个选项卡上显示徽章。对于我所使用的Android的viewbadger.jar文件Android ViewBadger 它做工精细用4个选项卡,Tabhost-设置标签中的徽章位置Android

TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs); 
    badge1 = new BadgeView(this, tabs, 1); 
    badge1.setText("155"); 
    badge1.setBadgePosition(BadgeView.POSITION_BOTTOM_RIGHT); 
    badge1.toggle(); 

enter image description here

当我在这里增加一个选项卡,将这个样子

enter image description here

我已经使用这些标志性

badge1.setPadding(left, top, right, bottom); 
    badge1.setTextSize(15); 
    badge1.setBadgeMargin(5,5); 
    badge1.setWidth(10); 
+0

http://kevinpelgrims.com/blog/2014/06/24/adding-a-badge-to -an-actionbar-tab – Shijil 2014-09-29 11:51:09

回答

1

设置TabWidget上的徽章将只显示可绘制TabWidget和边界之间可用空间的徽章,因此添加更多选项卡将压缩徽章。除了使用setIndicator(字符串,可绘制),试试这个:

ImageView iv = new ImageView(this); 
    iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    iv.setImageResource(R.drawable.whoseinterested); 
    whosespec.setIndicator(iv); 
    Intent whoseIntent = new Intent(this, BlankActivity.class); 
    whosespec.setContent(whoseIntent); 

    tabHost.addTab(whosespec); 

    badge1 = new BadgeView(this, iv); 
    badge1.setText("155"); 
    badge1.setTextSize(15); 
    badge1.setBadgeBackgroundColor(Color.BLACK); 
    badge1.setTextColor(Color.WHITE); 
    badge1.toggle(); 

截图: screen

你可以在你的屏幕截图,在与徽章标签图像向中心转移清楚地看到。如果图像与其制表符边界之间没有空格,则不会正确显示。

来源:测试我自己。

+0

这是完美的答案和工作就像一个魅力@berserk非常感谢你 – 2014-09-30 13:05:23

+1

@AliAshiq很高兴它帮助:) – berserk 2014-09-30 13:42:33

1

喜在imageview的

设置徽章的视图选项卡中的孩子试试这个

TabWidget tabsw = (TabWidget) rootView.findViewById(android.R.id.tabs); 
     ViewGroup viewgroup = (ViewGroup) tabsw.getChildAt(0); 
     viewgroup.getChildCount(); 

     for (int i = 0; i < viewgroup.getChildCount(); i++) { 
      if (viewgroup.getChildAt(i) instanceof ImageView) { 
       ImageView new_name = (ImageView) viewgroup.getChildAt(i); 
       badge7 = new BadgeView(getActivity(),new_name); 
       badge7.setText("9"); 
       badge7.setTextSize(9); 
       badge7.setBadgeMargin(0,0); 
       badge7.setBadgePosition(BadgeView.POSITION_TOP_RIGHT); 
       badge7.toggle(); 

      } 
     } 
+0

http://stackoverflow.com/questions/26402748/tabhost-android-viewbadger-badge -issue 看看这个问题@calinbros – 2014-10-27 10:50:30