问题
I have developed an android app that mainly targets smartphones. However in tablet emulator I see that it works on android 3.x, too.
However there is one little problem. The user cannot open OptionsMenu when he clicks on the menu button. As you know on the smartphone a menu appears from the bottom. But on tablet nothing happens.
I have read this http://developer.android.com/guide/topics/ui/menus.html#options-menu but still cannot figure out how to manage this. My app has a custom action bar.
My code is quite straight forward. In main activity:
@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()) {
//...
}
}
And the prefs activity:
public class MdPrefsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(
MdSharedPrefs.PREFS_NAME);
addPreferencesFromResource(R.xml.prefs);
}
}
MdSharedPrefs class just contains some getters and setters to retrieve/write the pref values.
Any ideas how I can show the OptionsMenu on tablet?
回答1:
As the documentation said:
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 [...]
So for Android 3.0 or higher you can see only the menu items in the ActionBar
.
It is also important to notice that:
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.
But be aware that the ActionBar
is visible only if you don't have an application or activity theme that explicitly hides it like
android:theme="@android:style/Theme.Holo.NoActionBar"
回答2:
On a tablet, no hardware button that can be used to load the menus, you need to create two folders in your res: the first call it values-11 and the second call it values-14. Inside these folders, put these styles (styles.xml) that will replace your default basetheme in the values folder whenever devices of higher version are used:
Res/values-11
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
Res/values-14
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
来源:https://stackoverflow.com/questions/8841877/how-to-open-optionsmenu-on-tablet