我想在android中制作选项菜单,但它显示为上下文菜单。这是代码。
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Item1");
menu.add("Item2");
return true;
}
任何人都可以帮助我理解它显示为上下文菜单。 感谢
答案 0 :(得分:0)
试试这个
public static final int ADD_CATEGORY_INDEX1 = Menu.FIRST;
public static final int ADD_CATEGORY_INDEX2 = Menu.FIRST+1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADD_CATEGORY_INDEX1, 0, "item1");
menu.add(0, ADD_CATEGORY_INDEX2, 0, "item2");
return super.onCreateOptionsMenu(menu);
}
答案 1 :(得分:0)
如果要创建菜单,请执行以下操作:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
它显示为上下文菜单,因为您没有为菜单设置布局,并且您不会对其进行充气。使用提供的代码和类似的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Menu button - Home -->
<item
android:id="@+id/homeButton"
android:icon="@drawable/homeicon"
android:title="@string/homeButton"
android:textSize="10sp"
/>
<!-- Menu button - Location -->
<item
android:id="@+id/locationButton"
android:icon="@drawable/locbutton"
android:title="@string/locationButton"
android:textSize="10sp"
/>
<!-- Menu button - Dining -->
<item
android:id="@+id/diningButton"
android:icon="@drawable/diningbutton"
android:title="@string/diningButton"
android:textSize="10sp"
/>
<!-- Menu button - Top25 -->
<item
android:id="@+id/topXXVButton"
android:icon="@drawable/topxxv"
android:title="@string/topXXVButton"
android:textSize="10sp"
/>
这一切都应该让你前进。
希望它有所帮助!
答案 2 :(得分:0)
尝试这种方式:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.device_menu, menu);
setMenuBackground();
return true;
}
private void setMenuBackground() {
getLayoutInflater().setFactory(new LayoutInflater.Factory() {
public View onCreateView(final String name,final Context context,final AttributeSet attributeSet) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
final LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attributeSet);
//view.setBackgroundResource(R.drawable.menu_selector);
new Handler().post(new Runnable() {
public void run() {
view.setBackgroundResource(R.drawable.menu_selector);
}
});
return view;
} catch (final Exception e) {
Log.e("##Menu##","Could not create a custom view for menu: "+ e.getMessage(), e);
}
}
return null;
}
});
}
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuItem1:
//code for first menu item click
break;
case R.id.menuItem2:
//code for second menu item click
break;
}
return true;
}
这是menu_selector.xml(在drawable文件夹中):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/menu_pressed"/>
<item android:state_focused="true" android:drawable="@color/menu_focused"/>
<item android:drawable="@color/menu_normal"/>
</selector>
这是device_menu.xml(在布局文件夹中):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuItem1"
android:icon="@drawable/icon_for_item_1"
android:title="Item1" />
<item android:id="@+id/menuItem2"
android:icon="@drawable/icon_for_item_2"
android:title="Item2" />
</menu>
这是colors.xml(在“res”文件夹的“values”文件夹中):
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="menu_normal">#ff222222</color>
<color name="menu_focused">#ff444444</color>
<color name="menu_pressed">#ffcccccc</color>
</resources>