Android SubMenu
1968 ワード
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final int FONT_10 = 0x123;
final int FONT_12 = 0x124;
final int FONT_14 = 0x125;
final int PLAIN_ITEM = 0x126;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
private void findViews() {
textView = (TextView) findViewById(R.id.textView1);
}
// menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, PLAIN_ITEM, 0, " ");
SubMenu progMenu = menu.addSubMenu(" activity");
progMenu.setIcon(R.drawable.ic_launcher);
progMenu.setHeaderIcon(R.drawable.ic_launcher);
progMenu.setHeaderTitle(" ");
MenuItem menuItem = progMenu.add(" ");
menuItem.setIntent(new Intent(MainActivity.this, OtherActivity.class));
SubMenu fontMenu = menu.addSubMenu(" ");
fontMenu.setIcon(R.drawable.ic_launcher);
fontMenu.setHeaderIcon(R.drawable.ic_launcher);
fontMenu.setHeaderTitle(" ");
fontMenu.add(0, FONT_10, 0, "10 ");
fontMenu.add(0, FONT_12, 0, "12 ");
fontMenu.add(0, FONT_14, 0, "14 ");
return super.onCreateOptionsMenu(menu);
}
// item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FONT_10:
textView.setTextSize(10 * 2);
break;
case FONT_12:
textView.setTextSize(12 * 2);
break;
case FONT_14:
textView.setTextSize(14 * 2);
break;
case PLAIN_ITEM:
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}