Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 添加带导航抽屉的操作栏按钮?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了这个问题:我正在使用操作栏导航抽屉,但我想在菜单栏中添加一个按钮,就像这些按钮:
http://screenshots.en.sftcdn.net/blog/en/2013/06/notifications-b.png

这是我的xml文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://scheR_429_11845@as.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://scheR_429_11845@as.android.com/apk/res/android"
      xmlns:yourapp="http://scheR_429_11845@as.android.com/apk/res-auto" >
    <!-- Search,should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android@R_575_6964@="@String/action_search"
          yourapp:showAsAction="ifRoom"  />
</menu>

但我仍然不知道如何适应这两个部分,任何人都可以帮助我吗?

解决方法

在您的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);

这告诉活动您将要更改选项菜单.

然后:

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

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

大佬总结

以上是大佬教程为你收集整理的android – 添加带导航抽屉的操作栏按钮?全部内容,希望文章能够帮你解决android – 添加带导航抽屉的操作栏按钮?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。