我需要创建菜单的子菜单。当我点击菜单时,子菜单应显示如下图所示,当我点击调整菜单时,它应该打开子菜单。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
case R.id.gamepad:
Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
break;
case R.id.camera:
Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
break;
case R.id.video:
Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
break;
case R.id.email:
Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
menu xml
<item
android:id="@+id/phone"
android:title="phone"
android:icon="@drawable/phone"
myapp:showAsAction="ifRoom" />
<item
android:id="@+id/computer"
android:title="computer"
android:icon="@drawable/computer"
myapp:showAsAction="ifRoom" />
<item
android:id="@+id/gamepad"
android:title="gamepad"
android:icon="@drawable/gamepad"
myapp:showAsAction="ifRoom|withText" />
<item
android:id="@+id/camera"
android:title="camera"
android:icon="@drawable/camera"
myapp:showAsAction="ifRoom|withText" />
<item
android:id="@+id/video"
android:title="video"
android:icon="@drawable/video"
myapp:showAsAction="ifRoom|withText" />
<item
android:id="@+id/email"
android:title="email"
android:icon="@drawable/email"
myapp:showAsAction="ifRoom|withText" />
</menu>