当前位置:网站首页>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;
}
}
边栏推荐
- 本周小贴士#136:无序容器
- Audio device strategy audio device output and input selection is based on 7.0 code
- 第3章业务功能开发(用户登录)
- 阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
- [Huang ah code] Why do I suggest you choose go instead of PHP?
- The top of slashdata developer tool is up to you!!!
- Numberpick的功能和用法
- 【可信计算】第十二次课:TPM授权与会话
- 99% of users often make mistakes in power Bi cloud reports
- Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination
猜你喜欢
Biped robot controlled by Arduino
datepicket和timepicket,日期、时间选择器的功能和用法
99%的人都不知道|私有化部署还永久免费的即时通讯软件!
Matplotlib绘制三维图形
第2章搭建CRM项目开发环境(搭建开发环境)
第3章业务功能开发(实现记住账号密码)
User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions
本周小贴士#136:无序容器
【解惑】App处于前台,Activity就不会被回收了?
redis主从、哨兵主备切换搭建一步一步图解实现
随机推荐
Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
MRS离线数据分析:通过Flink作业处理OBS数据
使用 xml资源文件定义菜单
99% of users often make mistakes in power Bi cloud reports
【OKR目标管理】案例分析
【信息安全法律法規】複習篇
Leetcode brush questions day49
LeetCode 535(C#)
Several best practices for managing VDI
百度地图自定义样式向右拖拽导致全球地图经度0度无法正常显示
简单的loading动画
From Devops to mlops: how do it tools evolve to AI tools?
Ratingbar的功能和用法
阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
Sator推出Web3游戏“Satorspace” ,并上线Huobi
Notes on installing MySQL in centos7
【可信计算】第十二次课:TPM授权与会话
Dateticket and timeticket, functions and usage of date and time selectors
在窗口上面显示进度条
ViewSwitcher的功能和用法