当前位置:网站首页>Define menus using XML resource files
Define menus using XML resource files
2022-07-07 17:37:00 【XLMN】
Use xml Resource file definition menu
The new version android It's hidden menu Key
menu file
xml file
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“xml Set file definition menu ”
android:id="@+id/textview001"/>
java file
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);
// Register context menu for text box
registerForContextMenu(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=new MenuInflater(this);
// To fill in R.menu.text_menu Corresponding menu , To add to menu in
inflater.inflate(R.menu.text_menu,menu);
return super.onCreateOptionsMenu(menu);
}
// This method is triggered when the context menu is created
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater=new MenuInflater(this);
// To fill in R.menu.context,menu Corresponding menu , To add to menu in
inflater.inflate(R.menu.context,menu);
menu.setHeaderIcon(R.drawable.mia5);
menu.setHeaderTitle(" Please select a background color ");
super.onCreateContextMenu(menu, v, menuInfo);
}
// This method is triggered when a menu item in the context menu is clicked
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
// Check this menu 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;
}
// The callback method after the menu item is clicked
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.isCheckable()){
// Check this menu item
item.setChecked(true);
}
// Judge which item is clicked , And make targeted response
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," You clicked the normal menu item ",Toast.LENGTH_SHORT);
toast.show();
break;
}
return true;
}
}
边栏推荐
- Mysql 索引命中级别分析
- Examen des lois et règlements sur la sécurité de l'information
- Flash build API service
- 浅谈 Apache Doris FE 处理查询 SQL 源码解析
- With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
- 【TPM2.0原理及应用指南】 16、17、18章
- 【网络攻防原理与技术】第6章:特洛伊木马
- 【OKR目标管理】案例分析
- Flask build API service SQL configuration file
- Sator a lancé le jeu web 3 "satorspace" et a lancé huobi
猜你喜欢
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
管理VDI的几个最佳实践
【可信计算】第十三次课:TPM扩展授权与密钥管理
User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions
imageswitcher的功能和用法
Dateticket and timeticket, functions and usage of date and time selectors
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
【TPM2.0原理及应用指南】 9、10、11章
mysql官网下载:Linux的mysql8.x版本(图文详解)
【信息安全法律法规】复习篇
随机推荐
Problems encountered in Jenkins' release of H5 developed by uniapp
L1-027 出租(Lua)
简单的loading动画
如何在软件研发阶段落地安全实践
SlashData开发者工具榜首等你而定!!!
使用Stace排除故障的5种简单方法
DNS 系列(一):为什么更新了 DNS 记录不生效?
企业经营12法的领悟
Function and usage of numberpick
【OKR目标管理】案例分析
Several best practices for managing VDI
Functions and usage of imageswitch
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
First in China! Todesk integrates RTC technology into remote desktop, with clearer image quality and smoother operation
MySQL usage notes 1
无法链接远程redis服务器(解决办法百分百)
Flash build API Service - generate API documents
The top of slashdata developer tool is up to you!!!
Smart logistics platform: make overseas warehouses smarter
大笨钟(Lua)