2013-02-23 73 views
0

添加badgeview我想在動作條菜單項上ActionBarSherlock菜單項

添加badge但數字圖標沒顯示。

這裏是我迄今

public class Main extends SherlockFragmentActivity 
{ 
    private Fragment menuFrag=null; 
    private MenuItem menuMsg=null; 
    private BadgeView badge=null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    //Do my stuff... 
    initUI(); 
    } 

    private void initUI() 
    { 
    FragmentManager fm=getSupportFragmentManager(); 
    FragmentTransaction ft=fm.beginTransaction(); 
    menuFrag=fm.findFragmentByTag("f1"); 
    if(menuFrag==null) 
    { 
     menuFrag=new MenuFragment(); 
     ft.add(menuFrag, "f1"); 
    } 
    ft.commit(); 

    // badge=new BadgeView(Main.this, (View)menuMsg); //Not working 
    badge=new BadgeView(Main.this, menuMsg.getActionView()); //Not working as well 
    badge.setBackgroundResource(R.drawable.badge_ifaux); 
    badge.setTextSize(10); 
    badge.setBadgeMargin(2); 
    badge.setText("1"); 
    badge.show(); 
    } 

    private class MenuFragment extends SherlockFragment 
    { 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 
    } 

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
    { 
     menu.add("Cloud").setIcon(R.drawable.icon_cloud).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 
     menu.add("List").setIcon(R.drawable.icon_list).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 
     menuMsg=menu.add("Msg"); 
     menuMsg.setIcon(R.drawable.icon_msg).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     Toast.makeText(Main.this, "Got click: " + item.toString(), Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
    } 
} 

哪裏做錯了呢?

+0

我沒有看到你所期望的發生在您的代碼中所做的一切就是創建一個「BadgeView」。如果你閱讀了這個庫的文檔,你會發現作者明確表示你不能這麼做(所以你可以隨意擴展它來添加所需的行爲)。此外,你不應該這樣做,因爲它不是一個Android特定的設計模式。 – Luksprog 2013-02-23 09:55:30

回答

1

RRTW,

您使用本身不支持徽章動作條菜單項目庫。

https://github.com/jgilfelt/android-viewbadger/commit/e08c3a78cb92c0c8587790b15e73434f972912cf

然而,這並不意味着你不能得到它的工作。

的設置將是如下(這裏假設你已經在你的項目中viewbager庫設置)

(1)onCreateOptionsMenu - >(2)添加R.menu.your_place_holder_item - >(3)setActionView用自定義xml佈局 - >(4)findViewById的MenuItem對象來獲取您的按鈕/視圖來設置徽章。

1)設置你onCreateOptionsMenu並創建了R.menu.actionbar_menu_messages

R.menu.actionbar_menu_messages

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     > 
    <item android:showAsAction="ifRoom" android:icon="@drawable/action_bar_pk_content_email" 
     android:id="@+id/menuMessages" android:title="More"></item> 

</menu> 

onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 
    MenuInflater inflater = getSupportMenuInflater(); //If you are using the support library otherwise use: getMenuInflater(); 
    inflater.inflate(R.menu.actionbar_menu_messages, menu); 
    this.setupMessagesBadge(menu.findItem(R.id.menuMessages)); //This is part of step 2 
    return true; 
} 

2)定義了common_messages_indicator

R.layout.common_messages_indicator:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="64dp" 
      android:layout_height="fill_parent" 
      android:paddingTop="10dp" 
      android:gravity="center"> 
    <ImageView 
     android:id="@+id/imgMessagesIcon" 
     android:layout_width="32dp" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:scaleType="fitCenter" 
     android:src="@drawable/messages_button" 
     android:background="@android:color/transparent" 
     android:focusable="false" 
     /> 

    </FrameLayout> 

執行setActionView到自定義XML佈局添加到

的ActionView
private void setupMessagesBadge(final MenuItem msgItem) { 
    msgItem.setActionView(R.layout.common_messages_indicator); 

    if(msgItem.getActionView().findViewById(R.id.imgMessagesIcon) != null) 
    { 
     ImageView imgMessagesIcon = ((ImageView)msgItem.getActionView().findViewById(R.id.imgMessagesIcon)); 

     imgMessagesIcon.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //Your click on the action bar item will be captured here 
      } 
     }); 
     int badgeCnt = 20;// Add your count here 
     if(messageCenterBadge == null && badgeCnt > 0) 
     { 
      //imgMessagesIcon is the imageview in your custom view, apply the badge to this view. 
      messageCenterBadge = new BadgeView(this, imgMessagesIcon); 
      messageCenterBadge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT); 
      messageCenterBadge.setBadgeMargin(0); 
      messageCenterBadge.setTextSize(12); 
      messageCenterBadge.setText(String.valueOf(badgeCnt)); 
      messageCenterBadge.show(); 
     } 
     else if(messageCenterBadge != null && badgeCnt > 0) 
     { 
      messageCenterBadge.setText(String.valueOf(badgeCnt)); 
      messageCenterBadge.show(); 
     } 
     else if(messageCenterBadge != null && badgeCnt == 0) { 
      messageCenterBadge.hide(); 
     } 
    } 
} 
+0

什麼是messageCenterBadge變量和appState.GetIsDisplayMessageCenter()?我如何初始化它? – exodream 2014-06-27 10:16:58

+0

'messageCenterBadge' nvm我明白了。仍然是什麼'appState'變量和'GetIsDisplayMessageCenter()'方法? – exodream 2014-06-27 10:18:43

+0

我已經從上面的示例中刪除了該方法。這是針對我正在研究的一個應用程序。不需要實施徽章。 – hylander0 2014-06-27 13:45:55

相關問題