【问题标题】:add actionbar button with navigation drawer?添加带有导航抽屉的操作栏按钮?
【发布时间】:2013-11-13 23:50:16
【问题描述】:

我遇到了这个问题:我正在使用操作栏导航抽屉,但我想在菜单栏中另外添加一个按钮,比如这些按钮: http://screenshots.en.sftcdn.net/blog/en/2013/06/notifications-b.png

这是我的 xml 文件:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

<FrameLayout
     android:id="@+id/content_frame"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
     <!-- The navigation drawer -->
<ListView
     android:id="@+id/left_drawer"
     android:layout_width="240dp"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     android:background="#111"
     android:choiceMode="singleChoice"
     android:divider="@android:color/transparent"
     android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

所以,我知道添加按钮是这样的:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
</menu>

但我仍然不知道如何安装这两个部分,有人可以帮助我吗?

【问题讨论】:

    标签: android button android-actionbar drawer


    【解决方案1】:

    在您的 Activity_Main 或您的主要活动中,您调用:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.YOUR_MENU, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    

    YOUR_MENU 是您想要的选项菜单/操作栏。

    如果您想添加另一个按钮,或者根据 frame_layout/drawer 布局中的特定片段个性化按钮,然后在您的片段中,将其添加到 onCreateView 中:

    setHasOptionsMenu(true);
    

    这告诉 Activity 你将要更改选项菜单。

    然后:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.YOUR_NEW_MENU, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    

    YOUR_NEW_MENU 的任何项目都将添加到选项菜单/操作栏。

    【讨论】:

      【解决方案2】:

      您是否将此菜单项映射到您的活动?如果不是,您需要在您的活动中覆盖 onCreateOptionsMenu 并在此处对其进行充气。

      您可以在菜单上阅读此tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多