Android – the action bar displays only one item next to the search view

I want to have a search view and two buttons in the action bar, so I created menu / options_ menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
    android:title="Search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView" />

<item
    android:title="Route"
    android:id="@+id/action_route"
    android:icon="@drawable/route"
    android:showAsAction="always" />

<item
    android:title="Cancel"
    android:id="@+id/action_cancel"
    android:icon="@drawable/cancel"
    android:showAsAction="always" />

</menu>

I inflate it in my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    return true;
}

That's what I got:

As you can see, only the first item ("route") is displayed. If I make the icon search widget, I can see the second item ("Cancel"):

    //searchView.setIconifiedByDefault(false);

However, I want to expand the search widget, and both items are visible. Is it possible?

to update

I try to make search widgets visible by setting Android: orderincategory = "1" to "route" and "Cancel" and Android: orderincategory = "2":

This is almost what I want. The problem is that I want to search widgets first (leftmost), and then these two items. Is it possible?

Update 2

I tried the "recommend" method. I set the following for searchview:

android:showAsAction="always|collapseActionView"

After clicking the search icon, I get the extended view:

Two questions:

>The (secondary) navigation UI occupies the left room, thus reducing the available space of searchview. > (primary) Android icon is displayed as part of the navigation UI. Can I get rid of it?

resolvent:

Create a demo and check:

menu.xml:

<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:title="Search"
        android:icon="@android:drawable/ic_search_category_default"
        app:actionLayout="@layout/searchview"
        app:showAsAction="always|collapseActionView"/>

    <item
        android:title="Route"
        android:id="@+id/action_route"
        android:icon="@mipmap/ic_launcher"
        app:showAsAction="always" />

    <item
        android:title="Cancel"
        android:id="@+id/action_cancel"
        android:icon="@mipmap/ic_launcher"
        app:showAsAction="always" />

</menu>

searchview.xml:

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

Java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        if(searchItem!=null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
        }

        return true;
    }

Edit Java as needed (always open searchview):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        searchItem.expandActionView();
        final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        if(searchItem!=null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
            // This listener use for handle back press if you want to go back without collapsing searchview
            MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {

                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    onBackPressed();
                    return false;
                }
            });
        }
        return true;
    }

Screenshot:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>