public class MenuDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menudemo);
/**
* A Button that acts as the view element for the popup menu.
*/
final Button btn = (Button) findViewById(R.id.popupMenuBtn);
/**
* Step 1: Create a new instance of popup menu
*/
final PopupMenu popupMenu = new PopupMenu(this, btn);
/**
* Step 2: Inflate the menu resource. Here the menu resource is
* defined in the res/menu project folder
*/
popupMenu.inflate(R.menu.color_menu);
/**
* Step 3: Call show() method on the popup menu to display the
* menu when the button is clicked.
*/
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupMenu.show();
}
});
/**
* Handle menu item clicks
*/
popupMenu.setOnMenuItemClickListener(
new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_red:
btn.setBackgroundResource(R.color.LightRed);
break;
case R.id.menu_blue:
btn.setBackgroundResource(R.color.DullBlue);
break;
case R.id.menu_green:
btn.setBackgroundResource(R.color.LightGreen);
break;
}
return true;
}
});
}
}
R.id.menu_red
menu_blue
menu_green
각각의 아이템에 대한 제어를 하고 싶습니다. 글자 크기라던가 배경 색깔등 ...