ActionBar SearchView未在横向模式下完全展开

时间:2013-04-07 21:10:35

标签: android search android-actionbar searchview

使用操作栏搜索界面时,窗口小部件会以纵向模式展开以占据整个屏幕宽度,但在横向模式下会停止缩短。

当用户输入搜索时,有没有办法在SearchView上设置扩展布局参数以完全填充操作栏?

见图:

SearchView text field not using the full width in landscape mode

注意:我目前没有使用ActionBar Sherlock

编辑: 这就是我为扩展整个宽度而采取的措施。

searchView.setOnSearchClickListener(new OnClickListener() {
    private boolean extended = false;

    @Override
    public void onClick(View v) {
        if (!extended) {
            extended = true;
            LayoutParams lp = v.getLayoutParams();
            lp.width = LayoutParams.MATCH_PARENT;
        }
    }

});

6 个答案:

答案 0 :(得分:16)

MenuItemCompat的SearchView有一个名为maxWidth的属性。

    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setMaxWidth(xxx);

使用屏幕宽度而不是xxx offcourse

答案 1 :(得分:4)

你有没有尝试过类似的东西?

editView.setOnKeyListener(new OnKeyListener() {
    private boolean extended = false;

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!extended && event.getAction() == KeyEvent.ACTION_DOWN) {
            extended = true;
            LayoutParams lp = v.getLayoutParams();
            lp.width = LayoutParams.MATCH_PARENT;
        }

        return false;
    }

});

答案 2 :(得分:0)

我找到了一个完整展开横向搜索视图的解决方案,并且在创建活动时也已经扩展了操作视图。这是如何运作的:

1.首先在res-menu文件夹中创建一个xml文件,例如:searchview_in_menu.xml。在这里,您将获得以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@+id/action_search"
              android:title="@string/search"
              android:icon="@android:drawable/ic_menu_search"
              android:actionLayout="@layout/searchview_layout" />
    </menu>

注意:“@ string / search” - 在res-strings.xml中看起来像这样:

    <string name="search">Search</string>

2.Second在res-layout文件夹中创建上面提到的布局(“@ layout / searchview_layout”)。新布局:searchview_layout.xml将如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <SearchView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/search_view_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

注意:这里我们设置搜索视图宽度以匹配其父级的宽度(android:layout_width =“match_parent”)

3.在MainActivity类或必须在onCreateOptionsMenu()方法中实现搜索视图的活动中,使用以下代码:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        //find the search view item and inflate it in the menu layout
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        //set a hint on the search view (optional)
        mSearchView.setQueryHint(getString(R.string.search));
        //these flags together with the search view layout expand the search view in the landscape mode
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                | MenuItem.SHOW_AS_ACTION_ALWAYS);
        //expand the search view when entering the activity(optional)
        searchItem.expandActionView();
        return true;
}

答案 3 :(得分:0)

将宽度布局参数调整为MATCH_PARENTSearchView将以纵向或横向完全展开

ViewGroup.LayoutParams layoutParams = mSearchView.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

答案 4 :(得分:0)

对于ActionBarCompat,请使用:

    MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
            | MenuItem.SHOW_AS_ACTION_ALWAYS);
    MenuItemCompat.expandActionView(searchItem);

答案 5 :(得分:0)

使用此(100%):

mSearchView = (SearchView) searchItem.getActionView();    
mSearchView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));