如何在Android片段中添加操作栏选项菜单
我正在尝试在Android Fragments中设置一个选项菜单<代码>操作栏选项菜单未显示在我的片段中 这是我的代码,我有如何在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
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();设置选项菜单(真);返回v我已经按你说的那样试过了。但它仍然不能在安卓姜饼上显示。4.4版的ABS也不适合我。相反,我走了一条不同的路线。我的片段在FragmentPagerAdapter中,我刚刚在onPageSelected()中动态添加和删除了一个菜单项。@iDroidExplorer set'app:showAsAction=“always”在您的菜单项中它对我有效,我使用的是SethasOptions菜单(true);在onCreateView中,而不是在onCreate中。调用SetHasOptions菜单(true);from onCreate解决了我的问题。它与与片段关联的活动无关?我的意思是,不必在活动中编写代码来显示菜单。只需将片段附加到活动。请尽快回复。这到底有什么帮助?您的意思是在步骤3中充气filter.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);
}