如何在android studio的导航栏标题中添加搜索字段?

时间:2017-06-29 08:48:00

标签: android android-layout android-navigation-drawer

2 个答案:

答案 0 :(得分:1)

只需在工具栏中添加一个编辑文本,如下所示:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:elevation="4dp">

<EditText
    android:id="@+id/searchEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</android.support.v7.widget.Toolbar>

答案 1 :(得分:0)

试试这个xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout     
  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:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.simple.activity.MainActivity">

 <android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/myAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.SearchView
                android:id="@+id/search_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@color/white"
                  android:queryHint="@string/search_hint"
                app:iconifiedByDefault="false"
                app:theme="@style/AppTheme.SearchView" />

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:background="@drawable/ic_arrow_forward_black_24dp"/>
         </FrameLayout>
       </android.support.design.widget.AppBarLayout>
   </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>