我遇到了这个问题:我正在使用操作栏抽屉导航,但我想在菜单栏中添加一个按钮,比如这些按钮: 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>
但我仍然不知道如何安装这两个部分,有人可以帮助我吗?
最佳答案
在您的 Activity_Main 或您的主要 Activity 中,您调用:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.YOUR_MENU, menu);
super.onCreateOptionsMenu(menu,inflater);
}
YOUR_MENU 是您想要的选项菜单/操作栏。
如果你想添加另一个按钮,或者根据 frame_layout/drawer 布局中的特定 fragment 个性化按钮,那么在你的 fragment 中,将其添加到 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 的任何项目都将添加到选项菜单/操作栏。
关于android - 添加带抽屉导航的操作栏按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966830/