如何自定义导航抽屉中的工具栏 activity?

How to customize toolbar in Navigation Drawer activity?

在我的 Android Studio 项目中创建导航抽屉 activity 后,我想在工具栏中添加一个搜索视图,而不是我的应用程序名称。我该怎么做?是否可以根据我的需要自定义工具栏或需要创建自定义工具栏?

如果您在创建新项目时选择了 Navigation Drawer activity Android Studio 将默认创建文件 app_bar_main.xml 文件

您应该可以找到默认给出的以下代码

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

将上面的代码替换为下面的代码以获取工具栏中的搜索视图

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">

<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content" /></android.support.v7.widget.Toolbar>