工具栏中的自定义布局

Custom layout in Toolbar

最近我从旧的 ActionBar 迁移到新的 Toolbar。

我有一个自定义 activity(实际上是我的主 activity 中的一个片段),我允许用户在其中单击工具栏中的图标,然后会出现一个搜索框 (在工具栏中),使用自定义布局。

问题是,在迁移之前它运行良好,但现在一旦我单击按钮并使用搜索框切换到自定义操作栏布局,该布局就会跨越所有屏幕,而不是工具栏的正常大小.

这是我用来替换工具栏的代码:

LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflator.inflate(R.layout.map_action_layout, null);

actionBar = ((DrawerActivity)getActivity()).getSupportActionBar();
actionBar.setCustomView(actionBarView);

这是地图操作布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
    android:id="@+id/autoComplete"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:hint="@string/search_adress_hint"
    android:imeOptions="actionSearch"
    android:inputType="textAutoComplete|textAutoCorrect"
    android:paddingRight="50dp"
    android:textColor="@android:color/white"
    android:textCursorDrawable="@null" >
    <requestFocus />
</AutoCompleteTextView>

<ImageButton
    android:id="@+id/clear_address"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="17dp"
    android:background="@drawable/ic_action_cancel"
    android:gravity="center_horizontal|center_vertical"
    android:scaleType="fitCenter" />

</RelativeLayout>

注意:无论我在上面的高度中输入什么值,无论是固定 dp 值还是 wrap_content,布局仍然跨越整个屏幕。

这是我正在替换的工具栏布局:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_scrollFlags="scroll|enterAlways"
    android:elevation="4dp"
    app:layout_collapseMode="pin">

</android.support.v7.widget.Toolbar>

工具栏位于 AppBarLayout 中。

有谁知道我做错了什么?我怎样才能达到我想要的结果?谢谢

将工具栏高度设置为 ?attr/actionBarSize 是标准的。

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize" />