簡體   English   中英

如何刪除出現在工具欄左側的默認菜單按鈕?

[英]How do I remove the default menu button that appears on the left of 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>

這是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>

您可能想查看https://stackoverflow.com/a/28291205/9891833此鏈接。 我相信這個鏈接是你想要實現的一個很好的例子。 如果你想要一條捷徑;

toolbar.getMenu().clear();

這可能會幫助您刪除按鈕。 我希望這些對你的情況有用。 祝你好運! :)

如果您想要具有可自定義選項的基於工具欄的設計,您可以繼續使用 AppBarLayout 包裝相對布局或任何其他根據您的興趣。 在這種情況下處理要容易得多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM