当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
企业即时通讯软件是什么?它有哪些优势呢?
第2章搭建CRM项目开发环境(数据库设计)
【OKR目标管理】案例分析
ViewSwitcher的功能和用法
【TPM2.0原理及应用指南】 5、7、8章
本周小贴士#136:无序容器
Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
使用popupwindow創建对话框风格的窗口
随机推荐
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
Sator launched Web3 game "satorspace" and launched hoobi
L1-025 正整数A+B(Lua)
到底有多二(Lua)
LeetCode 497(C#)
Mysql 索引命中级别分析
First in China! Todesk integrates RTC technology into remote desktop, with clearer image quality and smoother operation
【TPM2.0原理及应用指南】 1-3章
【可信计算】第十一次课:TPM密码资源管理(三) NV索引与PCR
What is cloud computing?
L1-023 输出GPLT(Lua)
本周小贴士#134:make_unique与私有构造函数
使用Stace排除故障的5种简单方法
第3章业务功能开发(用户访问项目)
第二十四届中国科协湖南组委会调研课题组一行莅临麒麟信安调研考察
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
Rpcms method of obtaining articles under the specified classification
【网络攻防原理与技术】第6章:特洛伊木马
Function and usage of numberpick
【信息安全法律法规】复习篇