首页 > 解决方案 > 如何删除工具栏左侧显示的默认菜单按钮?

问题描述

我正在尝试删除出现在工具栏左侧的默认菜单按钮,以便可以将其替换为自定义按钮。

这是我的工具栏的 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>

标签: javaandroidandroid-studioandroid-layout

解决方案


如果您想要具有可自定义选项的基于工具栏的设计,您可以根据您的兴趣继续使用 AppBarLayout 包装相对布局或任何其他布局。在这种情况下处理要容易得多


推荐阅读