问题
I am facing a few problems with the android option menu.
Here is my code:
Inside res folder, i created a menu folder containing menu.xml file with this below code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="@drawable/green"
android:id="@+id/icon" />
<item
android:id="@+id/text"
android:title="Text"/>
<item
android:id="@+id/icon1"
android:title="Icon and Text"
android:icon="@drawable/icon"/>
</menu>
And this is my SimpleOptionMenuActivity.java file:
public class SimpleOptionMenuActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon:
Toast.makeText(this, "Icon menu option is clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.text:
Toast.makeText(this, "Text menu option is clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.icon1:
Toast.makeText(this, "Icon and Text menu option is clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
1) On clicking the menu button of emulator, the menu is getting displayed like a ListView i.e. one above another, but i want the options to appear one beside another, 3 items per row, i.e. if there are four items then there will be two rows (which is usual, i don't know how to explain this properly).
2) The image is not getting displayed. I checked the R.java file, the icon is present inside the drawable inner class but still it's not getting displayed in the menu.
Please help me to solve the two problems.
回答1:
Change the API version from 15 to 11 or lower . Also , the image is not getting displayed because you might have selected an image too large for a certain drawable folder .
drawable-hdpi with 72 by 72 pixel icons
drawable-mdpi with 48 by 48 pixel icons
and
drawable-ldpi with 36 by 36 pixel icons
回答2:
Question 1:
in the AndroidManifest.xml, find something like the following: " uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" " remove the android:targetSdkVersion="16"
Question 2:
my menu.xml file like this:
<item android:id="@+id/about"
android:icon="@drawable/about"
android:title="@string/about"
android:showAsAction="ifRoom|withText" />
it works, show the image icon, you can try it.
来源:https://stackoverflow.com/questions/9773238/androidoption-menu-appearance-and-icon-not-getting-displayed