当前位置:网站首页>使用 xml资源文件定义菜单
使用 xml资源文件定义菜单
2022-07-07 15:40:00 【XLMN】
使用 xml资源文件定义菜单
新版本android已经隐藏menu按键
menu文件
xml文件
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“xml定文件定义菜单”
android:id="@+id/textview001"/>
java文件
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tv=findViewById(R.id.textview001);
//为文本框注册上下文菜单
registerForContextMenu(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=new MenuInflater(this);
//装填R.menu.text_menu对应的菜单,并添加到menu中
inflater.inflate(R.menu.text_menu,menu);
return super.onCreateOptionsMenu(menu);
}
//創建上下文菜单时触发该方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater=new MenuInflater(this);
//装填R.menu.context,menu对应的菜单,并添加到menu中
inflater.inflate(R.menu.context,menu);
menu.setHeaderIcon(R.drawable.mia5);
menu.setHeaderTitle("请选择背景色");
super.onCreateContextMenu(menu, v, menuInfo);
}
//上下文菜单中菜单项被单击时触发该方法
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
//勾选该菜单项
item.setChecked(true);
switch (item.getItemId()){
case R.id.red:
item.setChecked(true);
tv.setBackgroundColor(Color.RED);
break;
case R.id.green:
item.setChecked(true);
tv.setBackgroundColor(Color.GREEN);
break;
case R.id.blue:
item.setChecked(true);
tv.setBackgroundColor(Color.BLUE);
break;
} return true;
}
//菜单项被单击后的回调方法
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.isCheckable()){
//勾选该菜单项
item.setChecked(true);
}
//判断单击的是那个才行项,并针对性的做出响应
switch (item.getItemId()){
case R.id.font10:
tv.setTextSize(10*2);
break;
case R.id.font12:
tv.setTextSize(12*2);
break;
case R.id.font14:
tv.setTextSize(14*2);
break;
case R.id.font16:
tv.setTextSize(16*2);
break;
case R.id.font18:
tv.setTextSize(18*2);
break;
case R.id.red_font:
tv.setTextColor(Color.RED);
item.setChecked(true);
break;
case R.id.green_font:
tv.setTextColor(Color.GREEN);
item.setChecked(true);
break;
case R.id.blue_font:
tv.setTextColor(Color.BLUE);
item.setChecked(true);
break;
case R.id.plain_item:
Toast toast=Toast.makeText(MainActivity.this,"您单击了普通菜单项",Toast.LENGTH_SHORT);
toast.show();
break;
}
return true;
}
}
边栏推荐
- 【网络攻防原理与技术】第5章:拒绝服务攻击
- 服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
- 【饭谈】如何设计好一款测试平台?
- Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
- 如何在软件研发阶段落地安全实践
- LeetCode 403. Frog crossing the river daily
- [Seaborn] implementation of combined charts and multi subgraphs
- 智慧物流平台:让海外仓更聪明
- The server is completely broken and cannot be repaired. How to use backup to restore it into a virtual machine without damage?
- 电脑无法加域,ping域名显示为公网IP,这是什么问题?怎么解决?
猜你喜欢
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
如何在博客中添加Aplayer音乐播放器
Biped robot controlled by Arduino
Shallow understanding Net core routing
第3章业务功能开发(用户访问项目)
Sator launched Web3 game "satorspace" and launched hoobi
第3章业务功能开发(用户登录)
Seaborn data visualization
How to choose the appropriate automated testing tools?
What is cloud computing?
随机推荐
Mrs offline data analysis: process OBS data through Flink job
Flask搭建api服务-SQL配置文件
What is cloud computing?
Establishment of solid development environment
第二十四届中国科协湖南组委会调研课题组一行莅临麒麟信安调研考察
Sator推出Web3游戏“Satorspace” ,并上线Huobi
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
跟奥巴马一起画方块(Lua)
麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
【饭谈】Web3.0到来后,测试人员该何去何从?(十条预言和建议)
第3章业务功能开发(安全退出)
从DevOps到MLOps:IT工具怎样向AI工具进化?
Matplotlib绘图界面设置
LeetCode1051(C#)
Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
麒麟信安云平台全新升级!
centos7安装mysql笔记
Matplotlib绘制三维图形
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究