繁体   English   中英

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

[英]Add icon to Toolbar in Android

我正在尝试在我的可绘制目录中添加一个图标,该图标位于我的工具栏右侧,就在经典的“垂直三个点”之前。 这是我的 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:二进制 XML 文件第 14 行:二进制 XML 文件第 14 行:错误膨胀类项

这是我想在我的应用程序中重新创建的样式: 在此处输入图片说明

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

设置支持操作栏

  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