菜单的使用
测试菜单的时候看不到自己创建的菜单,需要点击Menu按钮才可以
1 /FragmentDemo1/res/menu中创建menu.xml ,菜单可以设置一些样式变得好看点
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- group1 --> <group android:id="@+id/group1"> <item android:id="@+id/mi1" android:title="西瓜"/> <item android:id="@+id/mi2" android:title="苹果"/> </group> <!-- group 2 --> <group android:id="@+id/group2"> <item android:id="@+id/mi3" android:title="苹果"/> <item android:id="@+id/mi4" android:title="橙子"/> </group> </menu>
2 Activity中重写 添加菜单的方法并响应事件
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.mi1: break; case R.id.mi2: break; case R.id.mi3: // do sth break; case R.id.mi4: // do sth break; } Toast.makeText(RadioGroupActivity.this, "您选择了:"+item.getTitle(),Toast.LENGTH_SHORT).show() ; return super.onOptionsItemSelected(item); }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28