SearchView不会扩展全宽

时间:2015-07-16 13:47:45

标签: android

我有下一个结果:

enter image description here

  

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:actionLayout="@layout/my_search_view"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView"/>

</menu>
  

my_search_view.xml

<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxWidth="10000dp"/>
  

活动

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint("Поиск");
    searchView.setOnQueryTextListener(this);


    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));


    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    searchView.setLayoutParams(params);
    searchView.setIconified(false);

    MenuItemCompat.expandActionView(searchItem);
    return super.onCreateOptionsMenu(menu);
}

我希望X(关闭操作)位于屏幕右侧。

3 个答案:

答案 0 :(得分:28)

MenuItemCompat的SearchView有一个名为maxWidth的属性。以下是诀窍:

searchView.setMaxWidth( Integer.MAX_VALUE );

答案 1 :(得分:1)

我遇到了同样的问题。 Searchview没有完全扩展。 这是我如何解决问题

在我的styles.xml中,我为SearchView定义了一种样式

 <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="queryBackground">@color/light_gray_3</item>
    <item name="submitBackground">@color/gray</item>
    <item name="closeIcon">@android:drawable/ic_menu_close_clear_cancel</item>
    <item name="goIcon">@drawable/abc_ic_go_search_api_mtrl_alpha</item>
    <item name="searchHintIcon">@null</item>
    <item name="android:maxWidth">1000dp</item>
</style>

然后在我的工具栏样式中添加了一行来设置搜索视图的样式

 <style name="ThemeToolbar" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">@color/gray</item>
    <item name="colorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@color/gray</item>
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>

这是我在search_view_layout.xml中定义的searchview布局

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/rk.chkout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:searchHintIcon="@null"
android:maxWidth="10000dp" />

和menu.xml中的搜索项声明

 <item
    android:orderInCategory="2"
    android:id="@+id/action_search"
    android:icon="@drawable/search_icon"
    android:title="@string/search"
    app:actionLayout="@layout/search_view_layout"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"/>

答案 2 :(得分:0)

尝试将其从菜单定义中删除: app:actionViewClass =“ android.support.v7.widget.SearchView”

就我而言,它禁止在search_view_layout中加载自定义的SearchView。