ActionBar菜单包含文本,图标,文本

时间:2016-09-10 11:17:16

标签: android menu android-actionbar

大家好我想在动作栏上显示带有文字和图像的动作,但我只在溢出菜单中显示文字

我的menu.xml文件

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

<item
    android:id="@+id/action_show_ir_list"

    android:showAsAction="ifRoom"
    android:title="Cancel"/>


<item
    android:id="@+id/phone"
    android:title="Wowio"
    android:icon="@drawable/logo"
    android:showAsAction="ifRoom"
  />
  <!--android:icon="@drawable/info"-->

  <item
    android:id="@+id/computer"
    android:title="Done"

    android:showAsAction="ifRoom"
    />


    </menu>

在我的主要班级

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

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);

    switch(item.getItemId()){
        case R.id.phone:
             Toast.makeText(getBaseContext(), "You selected About App", Toast.LENGTH_SHORT).show();
            break;

        case R.id.computer:


            Toast.makeText(getBaseContext(), "You selected About Developer", Toast.LENGTH_SHORT).show();
            break;
        case R.id.action_show_ir_list:

            Toast.makeText(getBaseContext(), "Volume Set To Max", Toast.LENGTH_SHORT).show();
            break;

    }
    return true;

}

这里的每一件事都是在流动菜单中进来的,动作栏上没有任何内容 enter image description here

我尝试了互联网上的所有内容,但没有任何工作

我该怎么做才能在ActionBar上展示它们?

1 个答案:

答案 0 :(得分:0)

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView)    
    MenuItemCompat.getActionView(item);


    MenuItemCompat.setOnActionExpandListener(item,
            new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    // Do something when collapsed

                    return true; // Return true to collapse action view
                }

                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    // Do something when expanded
                    return true; // Return true to expand action view
                }
            });

}

而不是[yourapp]键入您的应用名称