如何在导航抽屉活动中自定义工具栏?

God*_*ryd 0 android android-studio android-toolbar

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

导航抽屉活动

Nav*_*hew 5

如果您在创建新项目时选择了导航抽屉活动,则默认情况下,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" />
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明