当前位置: 代码迷 >> Android >> android中的menu跟子menu小结
  详细解决方案

android中的menu跟子menu小结

热度:30   发布时间:2016-05-01 16:32:14.0
android中的menu和子menu小结
menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/Menu1"
android:title="Menu 1"
android:orderInCategory="1" />
<item android:id="@+id/Menu2"
android:orderInCategory="2"
android:title="Menu 2" />
<item android:id="@+id/Menu3"
android:orderInCategory="3"
android:title="Menu 3" />
<item android:id="@+id/submenu"
    android:title="Sub menu"
    android:orderInCategory="4">
    <menu>
        <item android:id="@+id/submenu1"
        android:title="Sub menu 1" />
        <item android:id="@+id/submenu2"
        android:title="Sub menu 2" />
    </menu>
   </item>
</menu>
public class MenuActivity extends Activity {
    /** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication())
.inflate(R.menu.menu, menu);
return(super.onPrepareOptionsMenu(menu));
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
        case R.id.Menu1:
        Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
        break;
        case R.id.Menu2:
        Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
        break;
        case R.id.Menu3:
        Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
        break;
        case R.id.submenu:
        Toast.makeText(this, "Sub menu", Toast.LENGTH_SHORT).show();
        break;
    }
return(super.onOptionsItemSelected(item));
}
}
  相关解决方案