简体   繁体   English

Xamarin Android中的工具栏未显示图标

[英]Toolbar in Xamarin Android not showing icons

I'm trying to create a toolbar to save items in my application. 我正在尝试创建一个工具栏以在我的应用程序中保存项目。 But my toolbar is not showing properly. 但是我的工具栏显示不正确。

It is showing like this 像这样显示 I want to display like this 我想这样显示

In my Fragment 在我的片段

    public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
    {
        inflater.Inflate(Resource.Menu.menu_RefuelingToolbar, menu);
    }

menu_RefuelingToolbar.xml menu_RefuelingToolbar.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context=".RefuelingFragment">
          <item android:id="@+id/button_Save"
                android:icon="@drawable/ic_done_white_24dp"
                android:showAsAction="always"
                android:title="Settings" />
    </menu>

If you extend AppCompatActivity instead of Activity like this : 如果您扩展AppCompatActivity而不是像这样的Activity

public class MainActivity : AppCompatActivity {...}//instead of Activity

In this case, the toolbar cant showing picture, the reason is that : 在这种情况下,工具栏无法显示图片,原因是:

When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace. 使用appcompat库时,菜单资源应引用app:名称空间中的showAsAction,而不是android:名称空间。 Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute. 同样,当不使用appcompat库时,应使用android:showAsAction属性。

I think the problem is that you are mixing Framework Activity and AppCompat menu. 我认为问题在于您正在混合使用Framework ActivityAppCompat菜单。

You should use AppCompatActivity with AppCompat Action bar and app:showAsAction ; 您应该将AppCompatActivityAppCompat Action barapp:showAsAction or Activity with android:showAsAction . android:showAsAction活动。

EDIT : Modify your code in menu_MGradeToolbar.xml like this : 编辑 :像下面这样修改menu_MGradeToolbar.xml的代码:

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   >
<item android:id="@+id/button_MGradeSave"
    android:icon="@drawable/ic_done_white_24dp"
    app:showAsAction="ifRoom"
    android:title="Save" />
</menu>

Here is the result. 是结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM