android-如何在平板电脑上打开OptionsMenu?
作者:互联网
我开发了一个主要针对智能手机的android应用.但是,在平板电脑模拟器中,我看到它也可以在android 3.x上运行.
但是,有一个小问题.用户单击菜单按钮时无法打开OptionsMenu.如您所知,在智能手机上,菜单从底部出现.但是在平板电脑上什么也没发生.
我已经阅读了http://developer.android.com/guide/topics/ui/menus.html#options-menu,但仍然无法弄清楚如何进行管理.我的应用程序有一个自定义操作栏.
我的代码很简单.在主要活动中:
@Override
public boolean onCreateOptionsMenu(Menu men) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, men);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//...
}
}
和prefs活动:
public class MdPrefsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(
MdSharedPrefs.PREFS_NAME);
addPreferencesFromResource(R.xml.prefs);
}
}
MdSharedPrefs类仅包含一些getter和setter来检索/写入pref值.
有什么想法可以在平板电脑上显示OptionsMenu吗?
解决方法:
如文档所述:
Items in the Options Menu are accessible in two distinct ways: the
MENU button or in the Action Bar (on devices running Android 3.0 or
higher).[…]
On Android 3.0 and higher, items from the Options Menu is placed in
the Action Bar, which appears at the top of the activity in place of
the traditional title bar. By default all items from the Options Menu
are placed in the overflow menu, which the user can open by touching
the menu icon on the right side of the Action Bar. However, you can
place select menu items directly in the Action Bar as “action items,”
for instant access […]
因此,对于Android 3.0或更高版本,您只能在ActionBar中看到菜单项.
注意以下几点也很重要:
Beginning with Android 3.0 (API level 11), the action bar is included
in all activities that use the Theme.Holo theme (or one of its
descendants), which is the default theme when either the
targetSdkVersion or minSdkVersion attribute is set to “11” or greater.
但是请注意,只有在没有显式隐藏它的应用程序或活动主题时,才可以看到ActionBar
android:theme="@android:style/Theme.Holo.NoActionBar"
标签:android-optionsmenu,android 来源: https://codeday.me/bug/20191101/1986514.html