简体   繁体   English

如何删除出现在工具栏左侧的默认菜单按钮?

[英]How do I remove the default menu button that appears on the left of my toolbar?

I'm trying to remove that default menu button that appears on the left of my toolbar, so I can replace it with a custom button.我正在尝试删除出现在工具栏左侧的默认菜单按钮,以便我可以将其替换为自定义按钮。

Here is the XML file for my toolbar:这是我的工具栏的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    tools:context="MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/AppBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/colorPrimary">

            <TextView
                android:id="@+id/appBarTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:letterSpacing="0.5"
                android:text="@string/toolbar_title"
                android:textColor="@color/black"
                android:textSize="24sp"
                tools:ignore="MissingPrefix" />


        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    <include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

And here is the content_main.xml file:这是content_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="MainActivity"
    tools:showIn="@layout/toolbar">

</androidx.constraintlayout.widget.ConstraintLayout>

you might want to check https://stackoverflow.com/a/28291205/9891833 this link.您可能想查看https://stackoverflow.com/a/28291205/9891833此链接。 I believe that this link is a good example of what you want to achieve.我相信这个链接是你想要实现的一个很好的例子。 If you want a shortcut;如果你想要一条捷径;

toolbar.getMenu().clear();

this might help you to remove the button.这可能会帮助您删除按钮。 I hope these work for your case.我希望这些对你的情况有用。 Best of luck!祝你好运! :) :)

If you want a toolbar based design with customizable option you can go ahead with AppBarLayout wrapping a relativelayout or any other as per your interest.如果您想要具有可自定义选项的基于工具栏的设计,您可以继续使用 AppBarLayout 包装相对布局或任何其他根据您的兴趣。 Handling is much easier in this case在这种情况下处理要容易得多

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

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