在 Android 中将图标添加到工具栏

Add icon to Toolbar in Android

我正在尝试添加一个图标,它位于我的可绘制目录中,位于我的工具栏右侧,就在经典 "vertical three dots" 之前。这是我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_toolbar"
        style="@style/HeaderBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_20sdp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="@dimen/_3sdp">

        <item
            android:id="@+id/miCompose"
            android:icon="@drawable/eraser"
            android:title="Compose"
            app:showAsAction="ifRoom"></item>
    </androidx.appcompat.widget.Toolbar>

    <TextView
        android:id="@+id/textList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/my_toolbar"
        android:layout_centerHorizontal="true"
        android:textSize="26dp" />

    <ListView
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/my_toolbar" />

</RelativeLayout>

问题是我在

行收到此错误

android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class item

这是我想在我的应用程序中重新创建的样式:

是的。您可以在菜单方法中使用图标

设置支持操作栏

  setSupportActionBar(toolbar)

override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.menu_logout, menu)
        return true
    }