Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/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片段中添加操作栏选项菜单_Android_Android Fragments_Android Actionbar_Android Optionsmenu - Fatal编程技术网

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

如何在Android片段中添加操作栏选项菜单,android,android-fragments,android-actionbar,android-optionsmenu,Android,Android Fragments,Android Actionbar,Android Optionsmenu,我正在尝试在Android Fragments中设置一个选项菜单操作栏选项菜单未显示在我的片段中 这是我的代码,我有oncreateoptions菜单()和onOptionSelected()函数。我的代码没有显示任何错误。但“选项”菜单不显示 package org.reachout; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; im

我正在尝试在Android Fragments中设置一个选项菜单<代码>操作栏选项菜单未显示在我的片段中

这是我的代码,我有
oncreateoptions菜单()
onOptionSelected()
函数。我的代码没有显示任何错误。但“选项”菜单不显示

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

}

您需要在
onCreate()
中调用
设置选项菜单(true)

为了向后兼容,最好将此调用尽可能晚地放在
onCreate()
的末尾,或者更晚地放在
onActivityCreated()
或类似的内容中


请参见:

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

<activity
android:name="your Fragment or activity"
android:label="@string/xxxxxx"
android:theme="@android:style/Theme.Holo" >

我来不及回答,但我认为这是另一个解决办法,这是 这里没有提到,所以张贴

步骤1:为要添加的菜单创建一个xml,就像我必须在我的操作栏上添加一个过滤操作一样,因此我创建了一个xml过滤器.xml。要注意的主线是android:orderInCategory,这将在您想要显示的任何地方首先或最后显示操作图标。还有一件事需要注意的是值,如果值较小,则首先显示,如果值较大,则最后显示

filter.xml

<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>
步骤3:现在添加方法onCreateOptions菜单,该方法将被覆盖为:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.filter, menu);  // Use filter.xml from step 1
    }
步骤4:现在添加onOptionsItemSelected方法,通过该方法,当您从操作栏中选择添加的操作图标时,您可以执行任何您想执行的操作:

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

谢谢但我已经在安卓4.0+上实现了这一点。但由于某些原因,我的Android Gingerbread 2.3.3及更高版本上没有。我已经更新了上面的代码。请帮助我在Android 2.3.3及更高版本中修复此问题。移动
onCreateView()末尾的
sethasOptions菜单(true)
。像
View v=充气机.inflate();设置选项菜单(真);返回vfilter.xml
R.menu.search\u result
从何而来?是的,它的
filter.xml
。在第3步中更改了xml的名称。考虑了很多,但不想在没有澄清的情况下编辑您的答案,因为我仍在缓慢地解决自己的问题。谢谢你的更新。
@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);
    }