页面布局资源
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/tv_menu"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="30sp"
android:layout_margin="30dp"
android:text="这是一段文字" />
<Button
android:id="@+id/bt_menu"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:text="弹出式菜单"
android:textSize="20sp"
/>
</LinearLayout>
选项菜单(三点菜单)
菜单资源可以通过菜单布局文件实现和使用代码设置 一般推荐使用菜单布局文件实现
布局资源
规范:res目录下新建menu文件夹用于存储菜单布局文件
菜单布局文件
<?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/menu_one"
android:orderInCategory="100"
android:title="选项一"
app:showAsAction="never"
/>
<item
android:id="@+id/menu_two"
android:orderInCategory="100"
android:title="选项二"
app:showAsAction="never"
/>
<item
android:id="@+id/menu_three"
android:orderInCategory="100"
android:title="选项三"
app:showAsAction="never"
/>
</menu>
代码实现
重写onCreateOptionsMenu(Menu menu)方法,在该方法中调用Menu对象来添加菜单或者子菜单。 重写onOptionsItemSelected(MenuItem item)方法响应菜单项的点击事件
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.item_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_one:
setTitle("菜单一");
myToast("你选择了菜单一");
break;
case R.id.menu_two:
setTitle("菜单二");
myToast("你选择了菜单二");
break;
case R.id.menu_three:
setTitle("菜单三");
myToast("你选择了菜单三");
break;
}
return super.onOptionsItemSelected(item);
}
private void myToast(String str){
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
效果
上下文菜单(长按菜单)
长按某个组件弹出窗口
代码实现
一、重写onCreateContextMenu(ContextMenu menu, View view ,ContextMenu.ContextMenuInfo menuInfo) 二、重写onContextItemSelected(MenuItem item)
public void onCreateContextMenu(ContextMenu menu, View view ,ContextMenu.ContextMenuInfo menuInfo){
menu.setHeaderTitle("上下文菜单");
menu.add(0,200,0,"剪切");
menu.add(0,201,0,"复制");
menu.add(0,202,0,"删除");
SubMenu subMenu = menu.addSubMenu(0,300,0,"分享到");
subMenu.add(0,203,0,"QQ");
subMenu.add(0,204,0,"微信");
subMenu.add(0,205,0,"钉钉");
super.onCreateContextMenu(menu,view,menuInfo);
}
public boolean onContextItemSelected(MenuItem item){
switch (item.getItemId()){
case 200:
Toast.makeText(MainActivity.this,"剪切",Toast.LENGTH_LONG).show();
break;
case 201:
Toast.makeText(MainActivity.this,"复制",Toast.LENGTH_LONG).show();
break;
case 202:
Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_LONG).show();
break;
case 203:
Toast.makeText(MainActivity.this,"QQ",Toast.LENGTH_LONG).show();
break;
case 204:
Toast.makeText(MainActivity.this,"微信",Toast.LENGTH_LONG).show();
break;
case 205:
Toast.makeText(MainActivity.this,"钉钉",Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
三、绑定事件给组件registerForContextMenu(mTvMenu);
TextView mTvMenu;
Button mBtMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvMenu = findViewById(R.id.tv_menu);
mBtMenu = findViewById(R.id.bt_menu);
registerForContextMenu(mTvMenu);
}
效果
弹出式菜单(弹出式菜单)
单击事件弹出菜单类似于提示框
代码实现
一、封装弹出菜单方法
public void showPopupMenu(){
PopupMenu popupMenu = new PopupMenu(MainActivity.this,mBtMenu);
popupMenu.getMenuInflater().inflate(R.menu.item_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_one:
setTitle("菜单一");
myToast("你选择了菜单一");
break;
case R.id.menu_two:
setTitle("菜单二");
myToast("你选择了菜单二");
break;
case R.id.menu_three:
setTitle("菜单三");
myToast("你选择了菜单三");
break;
}
return true;
}
});
popupMenu.show();
}
二、设置单击事件显示
TextView mTvMenu;
Button mBtMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtMenu = findViewById(R.id.bt_menu);
mBtMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
});
}
效果
完整代码
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView mTvMenu;
Button mBtMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvMenu = findViewById(R.id.tv_menu);
mBtMenu = findViewById(R.id.bt_menu);
registerForContextMenu(mTvMenu);
mBtMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.item_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_one:
setTitle("菜单一");
myToast("你选择了菜单一");
break;
case R.id.menu_two:
setTitle("菜单二");
myToast("你选择了菜单二");
break;
case R.id.menu_three:
setTitle("菜单三");
myToast("你选择了菜单三");
break;
}
return super.onOptionsItemSelected(item);
}
private void myToast(String str){
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
public void onCreateContextMenu(ContextMenu menu, View view ,ContextMenu.ContextMenuInfo menuInfo){
menu.setHeaderTitle("上下文菜单");
menu.add(0,200,0,"剪切");
menu.add(0,201,0,"复制");
menu.add(0,202,0,"删除");
SubMenu subMenu = menu.addSubMenu(0,300,0,"分享到");
subMenu.add(0,203,0,"QQ");
subMenu.add(0,204,0,"微信");
subMenu.add(0,205,0,"钉钉");
super.onCreateContextMenu(menu,view,menuInfo);
}
public boolean onContextItemSelected(MenuItem item){
switch (item.getItemId()){
case 200:
Toast.makeText(MainActivity.this,"剪切",Toast.LENGTH_LONG).show();
break;
case 201:
Toast.makeText(MainActivity.this,"复制",Toast.LENGTH_LONG).show();
break;
case 202:
Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_LONG).show();
break;
case 203:
Toast.makeText(MainActivity.this,"QQ",Toast.LENGTH_LONG).show();
break;
case 204:
Toast.makeText(MainActivity.this,"微信",Toast.LENGTH_LONG).show();
break;
case 205:
Toast.makeText(MainActivity.this,"钉钉",Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
public void showPopupMenu(){
PopupMenu popupMenu = new PopupMenu(MainActivity.this,mBtMenu);
popupMenu.getMenuInflater().inflate(R.menu.item_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_one:
setTitle("菜单一");
myToast("你选择了菜单一");
break;
case R.id.menu_two:
setTitle("菜单二");
myToast("你选择了菜单二");
break;
case R.id.menu_three:
setTitle("菜单三");
myToast("你选择了菜单三");
break;
}
return true;
}
});
popupMenu.show();
}
}
|