我想在 android 中制作选项菜单,但它显示为上下文菜单。这是代码。
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Item1");
menu.add("Item2");
return true;
}
谁能帮我理解它显示为上下文菜单。谢谢
我想在 android 中制作选项菜单,但它显示为上下文菜单。这是代码。
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Item1");
menu.add("Item2");
return true;
}
谁能帮我理解它显示为上下文菜单。谢谢
尝试这个
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);
}
如果要创建菜单,请执行以下操作:
@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"
/>
所有这一切都应该让你前进。
希望能帮助到你!
试试这个方法:
@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(在可绘制文件夹中):
<?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>