2013-04-17 47 views
3

我有tabbar 5个标签,我在第4个标签上添加了徽章。我想在服务器执行一些操作后更新我的徽章值。但不知道如何做到这一点。另外我想从不同的activities更新徽章值。在Tabbar如何在Android中更新标签上的徽章值?

TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs); 
badge = new BadgeView(context, tabs, 3); 
badge.setTextSize(12); 
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT); 
badge.setText(pref.getString("balance", "0")); 
badge.toggle(); 

代码片段添加徽章预先感谢您

+0

嗨SANGRAM帕蒂尔,你可以发布示例代码这里在标签上新增的徽章,和(的.jar)的文件正在使用的图书馆吗?给我链接该库文件 – Jayesh

+1

这里是示例代码。 https://github.com/jgilfelt/android-viewbadger –

+0

我试过一次,但当我设置图像在标签背景作为setIndicator(视图)然后徽章不能正确显示....是任何解决方案? – Jayesh

回答

5

TabActivity这样创建静态TabWidget对象选项卡,

public static TabWidget tabs; 

访问tabs对象从任何活动的操作后执行,更新balanceSharedPreferences。并使用以下代码片段。

在你的其他Activity

TabWidget tabs = TabActivity.tabs; 
badge = new BadgeView(context, tabs, 3); 
badge.setTextSize(12); 
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT); 
badge.setText(pref.getString("balance", "0")); 
badge.toggle(); 

希望这会对你有帮助。

+1

Thankx Amol,它为我工作。 –

+0

快乐是我的快乐编码:) –

+0

不工作在我的情况 –

0

为了节省你必须做这样的共同喜好。有点像你如何加载它。您需要使用相同的首选项。

 SharedPreferences prefs = mContext.getSharedPreferences("PREF_KEY", 0); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("balance", balance_value); 
     editor.commit(); 
0

分享我的一些尝试的

如果你的观点是布局这样

android:layout_width="0dp"<br> 
android:layout_height="wrap_content"<br> 
android:layout_weight="1"<br> 

BadgeView WIL空白你的看法。

如何解决?修补

android.view.ViewTreeObserver.OnGlobalLayoutListener 

这样的: -

ViewTreeObserver vto = container.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     container.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     int width = container.getMeasuredWidth(); 
     int height = container.getMeasuredHeight(); 

     LayoutParams params = target.getLayoutParams(); 
     params.width = width; 
     params.height = height - BadgeView.this.getHeight(); 
     target.setLayoutParams(params);    
    } 
});