菜单没有出现在Fragment中(menu not appearing in Fragment)

编程入门 行业动态 更新时间:2024-10-26 05:26:30
菜单没有出现在Fragment中(menu not appearing in Fragment)

我正在尝试将菜单放入我的应用程序中的片段中。 但是,当我运行它时,菜单不会出现。 我对在片段中显示菜单所涉及的步骤的理解(如果我错了或缺少某些东西,请纠正我)是你做了以下事情:

在res / menu目录中创建菜单资源文件。 覆盖onCreateOptionsMenu(Menu, MenuInflater)并在所述方法中,膨胀由菜单资源ID定义的布局。 通过在片段的onCreate方法中调用setHasOptionsMenu(true) ,通知片段管理器该片段应该接收对onCreateOptionsMenu的调用。

我已经编写了我的代码的简化版本,只包括(我相信)应该显示菜单的最低限度。 谁能告诉我这段代码中缺少什么?

这是我的菜单资源xml:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item_1" android:icon="@android:drawable/ic_menu_add" android:title="@string/menu_item_text" android:showAsAction="ifRoom|withText"/> </menu>

我的片段代码:

import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_menu, menu); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, parent, false); return v; } }

我的活动代码:

package com.bignerdranch.android.fragmentmenuexample; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new MainFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } }

我的清单:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bignerdranch.android.fragmentmenuexample" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

我在运行IceCreamSandwich的AVD Galaxy Nexus上运行它。

I'm attempting to put a menu into a fragment in my app. However, the menu isn't appearing when I run it. My understanding of the steps involved in making a menu display in a fragment (and please correct me if I'm wrong or missing something) is that you do the following:

Create a menu resource file in the res/menu directory. Override onCreateOptionsMenu(Menu, MenuInflater) and within said method, inflate the layout defined by the menu resource ID. Notify the fragment manager that this fragment should receive a call to onCreateOptionsMenu by calling setHasOptionsMenu(true) in the fragment's onCreate method.

I've written a reduced version of my code to only include the bare minimum of what (I believe) should show a menu. Can anyone tell me what's missing from this code?

Here is my menu resource xml:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item_1" android:icon="@android:drawable/ic_menu_add" android:title="@string/menu_item_text" android:showAsAction="ifRoom|withText"/> </menu>

My fragment code:

import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_menu, menu); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, parent, false); return v; } }

And my activity code:

package com.bignerdranch.android.fragmentmenuexample; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new MainFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } }

And my manifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bignerdranch.android.fragmentmenuexample" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

I'm running this on an AVD Galaxy Nexus running IceCreamSandwich.

最满意答案

问题解决了。 我的styles.xml文件包含以下内容:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

我添加了一个新的资源目录values-v14并添加了以下样式:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

Problem solved. My styles.xml file had the following:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

I added a new resource directory values-v14 and added the following style to it:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

更多推荐

本文发布于:2023-07-30 06:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现在   菜单   Fragment   menu   appearing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!