0

我想在 ActionBar MenuItem上添加一个徽章

但是数字图标没有显示。

这是我到目前为止所做的

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

我哪里做错了?

4

1 回答 1

1

RRTW,

您使用的库本身不支持标记操作栏菜单项。

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

然而,这并不意味着你不能让它工作。

设置如下(假设您已经在项目中设置了 viewbager 库)

(1) onCreateOptionsMenu --> (2) 添加一个R.menu.your_place_holder_item --> (3) setActionView与自定义 xml 布局 --> (4) MenuItem 对象的findViewById以获取您的按钮/视图来设置徽章。

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>

onCreateOptions菜单:

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();
        }
    }
}
于 2013-09-10T18:05:49.917 回答