如何使工具栏的搜索对话框大小

How to make Search Dialog size of a toolbar

所以我学会了如何使用 this tutorial 制作搜索对话框,但它看起来不太漂亮,我正在寻找修复它的正确方法。如您所见,搜索小部件并未完全覆盖工具栏,我认为通过使用常量 dp 值来缩短它并不是一个好方法(不是吗?)。第二件事是我不太清楚为什么工具栏一半是白色,一半是蓝色?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFF"
        android:minHeight="?attr/actionBarSize">

        <ImageButton
            android:id="@+id/search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:background="@drawable/ic_action_search" />
    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">

        <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_world" />

        </FrameLayout>

        <ListView
            android:id="@+id/left_drawer_list"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#2196F3"
            android:choiceMode="multipleChoice"
            android:dividerHeight="0dp" />
    </android.support.v4.widget.DrawerLayout>


</RelativeLayout>

样式:

<resources>

    <!-- Base application theme. -->
    <style name="Theme.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowActionBar">false</item>
    </style>

</resources>

编辑

感谢 Shujito 的回答,documentation 我成功了。如果有人感兴趣,这里是修改后的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    // retrieve the searchview here
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    return true;
}

您可以使用 SearchView 而不是搜索对话框,它更灵活,可以轻松嵌入到 xml 菜单中,并且适合操作栏的高度,试试这个:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/menu_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/desc_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>
</menu>

将其膨胀到您的 activity 或片段中,这是一个片段示例。然后您可以使用项目的 getActionView() 方法

检索 SearchView
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.workload, menu);
    MenuItem item = menu.findItem(R.id.menu_search);
    // retrieve the searchview here
    this.mSearchView = (SearchView) item.getActionView();
}