子菜单不会出现在模拟器中?

时间:2011-07-30 13:18:41

标签: android

package com.droidnova.android.howto.optionmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class SimpleOptionMenuActivity extends Activity {
 /** Called when the activity is first created. */
@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, "You pressed the icon!",               Toast.LENGTH_LONG).show();
                            break;
        case R.id.text:     Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
                            break;
        case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
                            break;
    }
    return true;
}

}

我正在尝试创建这个简单的菜单,它在模拟器(1.6版本)中加载,但没有子菜单显示

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
    android:icon="@drawable/icon" />
<item android:id="@+id/text"
    android:title="Text" />
<item android:id="@+id/icontext"
    android:title="Icon and text"
    android:icon="@drawable/icon" />

那是xml文件

1 个答案:

答案 0 :(得分:0)

我认为您需要在xml中的主菜单元素中创建另一个菜单元素:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu>
<item android:id="@+id/icon"
    android:icon="@drawable/icon" />
<item android:id="@+id/text"
    android:title="Text" />
<item android:id="@+id/icontext"
    android:title="Icon and text"
    android:icon="@drawable/icon" />
</menu>

子菜单here.

有一个简单的教程