step1:重写activity的onCreateOptionsMenu方法
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return true;
- }
step2: main.xml文件布局
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/action_cart"
- android:title="扫描"
- android:orderInCategory="100"
- android:showAsAction="always"/>
- </menu>
step3:如果监听按钮重写onOptionsItemSelected方法
- @Override
- public boolean onOptionsItemSelected(MenuItem item){
- switch (item.getItemId()) {
- case R.id.action_cart://监听菜单按钮
- break;
- }
- return super.onOptionsItemSelected(item);
- }
效果图如下:
时间: 2024-11-08 19:01:28