本文介绍了showAsAction = “总是"在工具栏中被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到工具栏后,菜单图标出现问题.虽然我设置了一个菜单项 android:showAsAction="always" 它没有显示图标,但我只能在点击弹出图标时找到它.

After switching to toolbar there is a problem with menu icons. Although I set for a menu item android:showAsAction="always" it does not show the icon, I can only find it clicking on the popup icon.

这是我的活动

public class myActivity extends AppCompatActivity{
  .........
  public void onCreate(....){
     .............
     Toolbar toolbar = (Toolbar) findViewById(....);
     setSupportActionBar(toolbar);
  }
  ............
  public boolean onCreateOptionsMenu(Menu menu{
            getMenuInflater().inflate(R.menu.menu, menu);

            return super.onCreateOptionsMenu(menu);
  }
  .............
}

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

     <item
       android:id="@+id/settings"
       android:icon="@drawable/settings"
       android:title="settings"
       android:showAsAction="always"
     />
    <item
       android:id="@+id/help"
       android:icon="@drawable/help"
       android:title="help"
       android:showAsAction="never"
     />
</menu>

设置和帮助图标都只在弹出菜单中.那么如何在工具栏上显示设置图标?

Both settings and help icons are only in popup menu. So how to show settings icon on toolbar?

推荐答案

android:showAsAction 替换为 app:showAsAction.您还需要在根元素中的现有 xmlns 项旁边添加 xmlns:app="http://schemas.android.com/apk/res-auto".

Replace android:showAsAction with app:showAsAction. You will also need to add xmlns:app="http://schemas.android.com/apk/res-auto" alongside your existing xmlns item in your root element.

这篇关于showAsAction = “总是"在工具栏中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 07:47