Android 操作栏项目总是出现在溢出菜单中
我正在使用从FragmentActivity派生的MainActivity,其中的片段表示每个选项卡 在操作栏中。在的文档中,我实现了一个拆分操作栏,其顶部有选项卡,其余的操作项位于操作栏的底部。 因为每个选项卡的片段都有自己特定的操作项,所以在调用片段时会加载一个表示这些操作的菜单。 这在一般情况下有效。但是,即使操作栏还有足够的空间,操作项始终显示在操作栏底部的溢出菜单中。实际上,没有可见的项目或文本占用空间 我正在使用支持v4库 主要活动Android 操作栏项目总是出现在溢出菜单中,android,android-fragments,android-actionbar-compat,splitactionbar,Android,Android Fragments,Android Actionbar Compat,Splitactionbar,我正在使用从FragmentActivity派生的MainActivity,其中的片段表示每个选项卡 在操作栏中。在的文档中,我实现了一个拆分操作栏,其顶部有选项卡,其余的操作项位于操作栏的底部。 因为每个选项卡的片段都有自己特定的操作项,所以在调用片段时会加载一个表示这些操作的菜单。 这在一般情况下有效。但是,即使操作栏还有足够的空间,操作项始终显示在操作栏底部的溢出菜单中。实际上,没有可见的项目或文本占用空间 我正在使用支持v4库 主要活动 public class MainActivity
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
TabNavigatorPagerAdapter tabNavigatorPagerAdapter;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app
tabNavigatorPagerAdapter = new TabNavigatorPagerAdapter(getSupportFragmentManager());
// Set up the action bar
final ActionBar actionBar = getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent
actionBar.setHomeButtonEnabled(false);
//force tabs at top and actions at bottom
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
// Specify that we will be displaying tabs in the action bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(tabNavigatorPagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab
// We can also use the ActionBar.Tab#select() to do this if we have a reference
// to the Tab
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
// Add Calendar activity
actionBar.addTab(actionBar.newTab().setText(R.string.calendar_activity).setTabListener(this));
// Add Grocery List activity
actionBar.addTab(actionBar.newTab().setText(R.string.grocery_list_activity).setTabListener(this));
// Add Search activity
actionBar.addTab(actionBar.newTab().setText(R.string.search_activity).setTabListener(this));
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class TabNavigatorPagerAdapter extends FragmentPagerAdapter {
public TabNavigatorPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// This is the Calendar section of the App
return new CalendarFragment();
default:
// The other sections of the app are dummy placeholders
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return "Section " + (position + 1);
}
}
// The Calendar fragment
public static class CalendarFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(R.string.calendar_activity);
setHasOptionsMenu(true);
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_calendar, menu);
}
}
}
显示
<?xml version="1.0" encoding="utf-8"?>
日历菜单xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.kikicorp.myfirstapp.MainActivity">
<item
android:id="@+id/qwe"
android:icon="@drawable/ic_launcher"
myfirstapp:showAsAction="ifRoom"
android:title="qwe">
</item>
<item
android:id="@+id/ee"
android:icon="@drawable/ic_action_edit"
myfirstapp:showAsAction="ifRoom"
android:title="edit">
</item>
<item
android:id="@+id/xx"
android:icon="@drawable/ic_action_new"
myfirstapp:showAsAction="ifRoom"
android:title="new">
</item>
<item
android:id="@+id/go_crazy"
android:icon="@drawable/ic_action_search"
myfirstapp:showAsAction="ifRoom"
android:title="@string/go_crazy_action">
</item>
</menu>
结果截图
您使用的是本机操作栏,这表明您继承的是
FragmentActivity
,而不是ActionBarActivity
。因此,myfirstapp:showAsAction
将被忽略。对本机操作栏使用android:showAsAction
如果您打算将
appcompat-v7
用于操作栏的后端口,则将您的类更改为继承自ActionBarActivity
,而不是FragmentActivity
当我使用安卓:showAsAction时,安卓Studio IDE会将此标记为错误,建议从我自己的命名空间(在本例中为myfirstapp)引用。当我忽略它并运行应用程序时,它的工作方式完全符合我的要求。谢谢你解决这个问题!太糟糕了,IDE给了我错误的建议…我使用myfirstapp:showAsAction,appcompat-v7,继承自ActionBarActivity,我看到了像图片一样的溢出菜单。如何强制在顶部栏中显示溢出菜单?@MARM:从清单中删除拆分操作栏whennarrow
。这不应该适用于现代版本的appcompat-v7
,无论如何,上次我检查时是这样的。@MARM:那么您的操作栏将与图片不一样,它在底部的操作栏部分显示了拆分操作栏和溢出菜单。您可能希望打开自己的堆栈溢出问题,在那里发布自己的清单和屏幕截图,以便人们可以帮助您解决特定问题。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.kikicorp.myfirstapp.MainActivity">
<item
android:id="@+id/qwe"
android:icon="@drawable/ic_launcher"
myfirstapp:showAsAction="ifRoom"
android:title="qwe">
</item>
<item
android:id="@+id/ee"
android:icon="@drawable/ic_action_edit"
myfirstapp:showAsAction="ifRoom"
android:title="edit">
</item>
<item
android:id="@+id/xx"
android:icon="@drawable/ic_action_new"
myfirstapp:showAsAction="ifRoom"
android:title="new">
</item>
<item
android:id="@+id/go_crazy"
android:icon="@drawable/ic_action_search"
myfirstapp:showAsAction="ifRoom"
android:title="@string/go_crazy_action">
</item>
</menu>