简体   繁体   English

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

[英]ActionBar SearchView not fully expanding in landscape mode

When using the action bar search interface, the widget expands to occupy the full width of the screen in portrait mode, but stops short in landscape mode. 使用操作栏搜索界面时,窗口小部件将以纵向模式展开以占据屏幕的整个宽度,但在横向模式下会停止缩短。

Is there a way to set the expansion layout params on the SearchView to fully fill the action bar when the user is typing a search? 有没有办法在SearchView上设置扩展布局参数,以便在用户键入搜索时完全填充操作栏?

See image: 见图:

SearchView文本字段在横向模式下不使用全宽

Note: I'm not currently using ActionBar Sherlock 注意:我目前没有使用ActionBar Sherlock

Edit: Here's what I did to get it to extend the full width. 编辑:这是我为了扩展全宽而做的。

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;
        }
    }

});

MenuItemCompat's SearchView has a property named maxWidth. MenuItemCompat的SearchView有一个名为maxWidth的属性。

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

use screen width instead of xxx offcourse 使用屏幕宽度而不是xxx offcourse

Did you try something like that? 你尝试过类似的东西吗?

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;
    }

});

I figured out a solution for fully expanding the search view in landscape and to also have the action view already expanded when the activity is created. 我想出了一个解决方案,可以在横向上完全展开搜索视图,并且在创建活动时也可以扩展操作视图。 Here how it works: 它是如何工作的:

1.First create an xml file in your res-menu folder called for example : searchview_in_menu.xml. 1.首先在res-menu文件夹中创建一个xml文件,例如:searchview_in_menu.xml。 Here you would have the following code: 在这里,您将拥有以下代码:

    <?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>

Note: "@string/search" - looks something like this in the res-strings.xml: 注意:“@ string / search” - 在res-strings.xml中看起来像这样:

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

2.Second create the layout referred above ("@layout/searchview_layout") in res-layout folder. 2.Second在res-layout文件夹中创建上面提到的布局(“@ layout / searchview_layout”)。 The new layout: searchview_layout.xml will look like this: 新布局: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"/>

Note: Here we are setting the search view width to match the width of its parent( android:layout_width="match_parent") 注意:这里我们设置搜索视图宽度以匹配其父级的宽度(android:layout_width =“match_parent”)

3.In your MainActivity class or in the activity that has to implement the Search View write in the onCreateOptionsMenu() method the following code: 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;
}

Adjust the width layout param to MATCH_PARENT , that way the SearchView will fully expand in either portrait or landscape 将宽度布局参数调整为MATCH_PARENT ,这样SearchView将以纵向或横向完全展开

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

For ActionBarCompat use this: 对于ActionBarCompat,使用此:

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

Use this (100%): 使用此(100%):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM