首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android片段中添加动作栏选项菜单

如何在Android片段中添加动作栏选项菜单
EN

Stack Overflow用户
提问于 2013-09-10 16:44:27
回答 3查看 113.6K关注 0票数 64

我正在尝试在Android Fragments中有一个选项菜单。ActionBar选项菜单未显示在我的片段中。

下面是我的代码,我同时拥有onCreateOptionsMenu()onOptionSelected()函数。我的代码没有显示任何错误。但未显示选项菜单。

代码语言:javascript
复制
package org.reachout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import org.general.R;

public class ViewMessageFragment extends Fragment {

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        return (LinearLayout)inflater.inflate(R.layout.viewmessages_tab_fragment_layout, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.askexperts_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       // handle item selection
       switch (item.getItemId()) {
          case R.id.action_settings:
             // do s.th.
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }   

}
EN

回答 3

Stack Overflow用户

发布于 2013-09-10 16:58:51

您需要在onCreate()中调用setHasOptionsMenu(true)

为了向后兼容,最好在onCreate()的末尾,甚至在onActivityCreated()或类似的东西中,尽可能晚地进行此调用。

请参阅:https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)

票数 94
EN

Stack Overflow用户

发布于 2015-08-11 15:38:33

我回答晚了,但我认为这是另一个解决方案,这里没有提到,所以张贴。

步骤1:创建你想要添加的菜单的xml,就像我必须在我的操作栏上添加一个过滤器操作,所以我已经创建了一个xml filter.xml。要注意的主要代码行是android:orderInCategory,这将在您想要显示的第一个或最后一个位置显示操作图标。还有一件事需要注意的是值,如果值小于,它将首先显示,如果值大于,它将在最后显示。

filter.xml

代码语言:javascript
复制
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >


    <item
        android:id="@+id/action_filter"
        android:title="@string/filter"
        android:orderInCategory="10"
        android:icon="@drawable/filter"
        app:showAsAction="ifRoom" />


</menu>

Step 2: In onCreate() method of fragment只需像前面提到的那样放入以下行,它负责回调onCreateOptionsMenu(Menu menu,MenuInflater inflater)方法,就像在Activity中一样。

代码语言:javascript
复制
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

步骤3:现在添加方法onCreateOptionsMenu,该方法将被覆盖,如下所示:

代码语言:javascript
复制
@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.filter, menu);  // Use filter.xml from step 1
    }

步骤4:现在添加onOptionsItemSelected方法,当您从actionBar中选择添加的操作图标时,您可以通过该方法实现您想要执行的任何操作

代码语言:javascript
复制
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_filter){
            //Do whatever you want to do 
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
票数 90
EN

Stack Overflow用户

发布于 2015-05-11 02:54:19

在AndroidManifest.xml中设置主题全息如下:

代码语言:javascript
复制
<activity
android:name="your Fragment or activity"
android:label="@string/xxxxxx"
android:theme="@android:style/Theme.Holo" >

票数 -18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18714322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档