简体   繁体   中英

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 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:

<?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. 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. Handling is much easier in this case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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