选项​​菜单上没有动作条显示 [英] Options Menu not displaying on ActionBar

查看:226
本文介绍了选项​​菜单上没有动作条显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在动作条显示菜单使用 ActionBarActivity

我收到选项菜单点击选项按钮,而不是显示在操作栏中的顶部。

我需要点击从2.3开始的选项菜单顶部的选项菜单中,而不是

我的code:

 公共类MainActivity扩展ActionBarActivity {

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    动作条动作条= getSupportActionBar();
    actionBar.setSubtitle(mytest的);
    actionBar.setTitle(测试);
    getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
    如果(savedInstanceState == NULL){
        。getSupportFragmentManager()的BeginTransaction()加(R.id.container,新PlaceholderFragment())提交()。
    }
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_action,菜单);
    返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例R.id.menu_item_one:
        Toast.makeText(这一点,点击搜索,Toast.LENGTH_SHORT).show();
        打破;

    默认:
        打破;
    }
    返回super.onOptionsItemSelected(项目);
}

/ **
 *包含一个简单的视图的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{

    公共PlaceholderFragment(){}

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
        返回rootView;
    }
}

}
 

我的XML文件:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

<项目
    机器人:ID =@ + ID / menu_item_one
    机器人:图标=@可绘制/ ic_launcher
    机器人:showAsAction =总是
    机器人:标题=@字符串/ action_settings/>
<项目
    机器人:ID =@ + ID / menu_item_two
    机器人:图标=@可绘制/ ic_launcher
    机器人:showAsAction =总是
    机器人:标题=@字符串/ action_settings/>

< /菜单>
 

解决方案

这是可能的,这是因为你的设备有一个选项按钮,将打开菜单,因此Android不画的启动器图标。

<一个href="http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/" rel="nofollow">http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/

I wanted to display Menu in ActionBar using ActionBarActivity.

I am getting options menu while clicking options button instead of displaying in the top of action bar.

I need the options menu at top instead of clicking the options menu from 2.3 onwards

My Code :

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setSubtitle("mytest");
    actionBar.setTitle("Testing");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_action, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_item_one:
        Toast.makeText(this, "clicked search", Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

My XML file :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menu_item_one"
    android:icon="@drawable/ic_launcher"
    android:showAsAction="always"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/menu_item_two"
    android:icon="@drawable/ic_launcher"
    android:showAsAction="always"
    android:title="@string/action_settings"/>

</menu>

解决方案

It is possible that this is because your device has an options button that would open the menu, so Android don't draw the launcher icon.

http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/

这篇关于选项​​菜单上没有动作条显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆