软件编程
位置:首页>> 软件编程>> Android编程>> Android标题栏最右边添加按钮的实例

Android标题栏最右边添加按钮的实例

作者:安辉就是我  发布时间:2021-08-09 07:08:07 

标签:Android,标题,按钮

Android标题栏最右边添加按钮的实例

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);
}  

效果图如下:

Android标题栏最右边添加按钮的实例

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/lowprofile_coding/article/details/40142843

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com