Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ActionBarSherlock菜单项上添加badgeview_Android_Actionbarsherlock_Badge - Fatal编程技术网

Android 在ActionBarSherlock菜单项上添加badgeview

Android 在ActionBarSherlock菜单项上添加badgeview,android,actionbarsherlock,badge,Android,Actionbarsherlock,Badge,我想在ActionBar菜单项上添加一个 但是数字图标没有显示出来 以下是我到目前为止所做的 public class Main extends SherlockFragmentActivity { private Fragment menuFrag=null; private MenuItem menuMsg=null; private BadgeView badge=null; @Override protected void onCreate(Bundle saved

我想在ActionBar菜单项上添加一个

但是数字图标没有显示出来

以下是我到目前为止所做的

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;
    }
  }
}
我哪里做错了

您正在使用的库不支持以本机方式标记Actionbar菜单项

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

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

(1) onCreateOptions菜单-->(2)添加一个R.menu。您的\u place\u holder\u项目-->(3)setActionView,带有自定义xml布局-->(4)查找MenuItem对象的dviewById,以获取设置徽章的按钮/视图

1) 设置OnCreateOptions菜单并在R.menu.actionbar\u菜单\u消息中创建一个

R.menu.actionbar\u menu\u消息

<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>
2) 定义了通用消息\u指示器

R.layout.常用消息\u指示器:

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

我看不出您希望发生什么,因为您在代码中所做的只是创建一个
BadgeView
。如果您阅读了该库的文档,您会发现作者明确表示您不能这样做(因此可以随意扩展它以添加所需的行为)。另外,您不应该这样做,因为它不是Android特定的设计模式。什么是
messageCenterBadge
变量和
appState.GetIsDisplayMessageCenter()
?如何初始化它?
messageCenterBadge
nvm我知道了。还有什么是
appState
变量和
GetIsDisplayMessageCenter()
方法?我已经从上面的示例中删除了该方法。它是针对我正在开发的一个应用程序的。无需实施标识。
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();
        }
    }
}