源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-08-14 16:22 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android标题栏最右边添加按钮的实例
[b]Android标题栏最右边添加按钮的实例[/b] [b]step1:重写activity的onCreateOptionsMenu方法 [/b]
@Override 
public boolean onCreateOptionsMenu(Menu menu){ 
  MenuInflater inflater = getMenuInflater(); 
  inflater.inflate(R.menu.main, menu); 
  return true; 
} 
[b]step2: main.xml文件布局[/b]
<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> 
[b]step3:如果监听按钮重写onOptionsItemSelected方法[/b]
@Override  
public boolean onOptionsItemSelected(MenuItem item){ 
  switch (item.getItemId()) { 
  case R.id.action_cart://监听菜单按钮 
     
    break; 
  } 
  return super.onOptionsItemSelected(item); 
}  
效果图如下: [img]http://files.jb51.net/file_images/article/201709/20179990145004.png?201789925[/img] 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部