首页 > 代码库 > 安卓学习-界面-ui-菜单
安卓学习-界面-ui-菜单
1.选项菜单和子菜单
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu m1=menu.addSubMenu("字体大小"); m1.addSubMenu(0,1,0,"10号字体"); m1.addSubMenu(0,2,0,"18号字体"); SubMenu m2=menu.addSubMenu("颜色"); m2.addSubMenu(0,3,0,"红色"); m2.addSubMenu(0,4,0,"绿色"); menu.add("普通菜单"); MenuItem eItem=menu.add("退出"); //直接绑定退出按钮 eItem.setIntent(new Intent(MainActivity.this, ExitActivity.class)); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if(item.getItemId()==1){ Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==2){ Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show(); }else if(item.getItemId()==3){ Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==4){ Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show(); } return super.onMenuItemSelected(featureId, item); }}
2.上下文菜单
和上面的一样,只不过这个菜单可以绑定一个组件,创建完后必须调用registerForContextMenu(btn)来绑定组件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="46dp" android:layout_marginTop="24dp" android:text="长按我出菜单" /></RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); registerForContextMenu(btn); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { SubMenu m1=menu.addSubMenu("字体大小"); m1.addSubMenu(0,1,0,"10号字体"); m1.addSubMenu(0,2,0,"18号字体"); SubMenu m2=menu.addSubMenu("颜色"); m2.addSubMenu(0,3,0,"红色"); m2.addSubMenu(0,4,0,"绿色"); menu.add("普通菜单"); MenuItem eItem=menu.add("退出"); //直接绑定退出按钮 eItem.setIntent(new Intent(MainActivity.this, ExitActivity.class)); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { if(item.getItemId()==1){ Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==2){ Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show(); }else if(item.getItemId()==3){ Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==4){ Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show(); } return super.onContextItemSelected(item); }}
3.采用XML来配置菜单
在menu目录里新建memu1.xml
用下面语句调用
MenuInflater m=new MenuInflater(this);
m.inflate(R.menu.memu1, menu);
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="字体大小"> <menu> <item android:title="10号字体" android:id="@+id/item1"/> <item android:title="18号字体" android:id="@+id/item2"/> </menu> </item> <item android:title="颜色"> <menu> <item android:title="红色" android:id="@+id/item3"/> <item android:title="绿色" android:id="@+id/item4"/> </menu> </item> <item android:title="普通菜单"/> <item android:title="退出" android:id="@+id/item5"/></menu>
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater m=new MenuInflater(this); m.inflate(R.menu.memu1, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if(item.getItemId()==R.id.item1){ Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==R.id.item2){ Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show(); }else if(item.getItemId()==R.id.item3){ Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==R.id.item4){ Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==R.id.item5){ startActivity(new Intent(MainActivity.this,ExitActivity.class)); } return super.onMenuItemSelected(featureId, item); }}
4.PopupMenu
xml文件就用上面的menu1.xml
MainActivity.java
public class MainActivity extends Activity { Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { MenuInflater in=new MenuInflater(MainActivity.this); PopupMenu menu=new PopupMenu(MainActivity.this, btn1); in.inflate(R.menu.memu1, menu.getMenu()); menu.setOnMenuItemClickListener(new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { if(item.getItemId()==R.id.item1){ Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==R.id.item2){ Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show(); }else if(item.getItemId()==R.id.item3){ Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==R.id.item4){ Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show(); } else if(item.getItemId()==R.id.item5){ startActivity(new Intent(MainActivity.this,ExitActivity.class)); } return false; } }); menu.show(); } }); }}
安卓学习-界面-ui-菜单
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。