简体   繁体   English

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

[英]Add icon to Toolbar in Android

I'm trying to add an icon, which is located into my drawable directory, to the right of my toolbar, right before the classic "vertical three dots".我正在尝试在我的可绘制目录中添加一个图标,该图标位于我的工具栏右侧,就在经典的“垂直三个点”之前。 This is my xml:这是我的 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>

The problem is that I get this error on the line问题是我在线时收到此错误

android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class item android.view.InflateException:二进制 XML 文件第 14 行:二进制 XML 文件第 14 行:错误膨胀类项

This is the style I would like to recreate in my app:这是我想在我的应用程序中重新创建的样式: 在此处输入图片说明

Yes.是的。 You can use icon in menu method您可以在菜单方法中使用图标

set Support Action Bar设置支持操作栏

  setSupportActionBar(toolbar)

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

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

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