Android Menu
一.OptionMenu
- 创建Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu1" android:title="点赞" android:visible="true"
android:icon="@mipmap/fangda" app:showAsAction="always"></item>
<item android:id="@+id/menu2" android:title="收藏" android:visible="true"
android:icon="@mipmap/xin" app:showAsAction="never"></item>
<item android:id="@+id/menu3" android:title="举报" android:visible="true"
android:icon="@mipmap/iphone" app:showAsAction="ifRoom"></item>
<item android:id="@+id/menu4" android:title="反馈" android:visible="true"
android:icon="@mipmap/address" app:showAsAction="withText"></item>
</menu>
- 重写方法 onCreateOptionsMenu、onOptionsItemSelected
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this, "选择了"+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "选择了"+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
二.ContextMenu
- 注册上下文菜单,调用registerForContextMenu()方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context);
editText = findViewById(R.id.edit_name);
linearLayout = findViewById(R.id.liner_popMenu);
//注册上下文菜单
registerForContextMenu(editText);
}
- 创建上下文菜单并添加事件,重写以下两个方法:
onCreateContextMenu()
onContextItemSelected()
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//构建menu对象
getMenuInflater().inflate(R.menu.option_menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
//item为当前选中的菜单项
editText.setText(item.getTitle());
return super.onContextItemSelected(item);
}
三.PopupMenu
PopupMenu特点:点击控件触发菜单弹出
步骤:
1.创建PopupMenu对象,并指定作用的控件对象
2.重写menu的事件,setOnMenuItemClickListener()
3.调用show()显示menu
PopupMenu menu = new PopupMenu(getApplicationContext(),linearLayout);
menu.inflate(R.menu.option_menu);
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ContextActivity.this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
menu.show();
四.Activity布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContextActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:visibility="gone">
<TextView
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入你的选择:"
android:textSize="20sp"
android:textColor="@android:color/holo_blue_dark"/>
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入帐户"
android:layout_marginTop="20dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/liner_popMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/bq1"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="昨天北京无新增病例,全国目前仅确认10位病例"
android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
</LinearLayout>