Push icons away when expandig searchview in android toolbar

﹥>﹥吖頭↗ 提交于 2019-12-06 03:57:40

问题


I'm facing this particular behaviour and I don't know how to fix it..

In my activity I have this toolbar:

And when you click on the search icon it expands and pushes the other icon to out of the view.

I want to know how could I keep the icon on the right and reduce the search view space.

This is my menu layout:

<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="@string/buscar"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always" />

<item
    android:id="@+id/alimentos_basket"
    android:orderInCategory="1"
    android:title="@string/alimentos_anadidos"
    app:showAsAction="always" />

In my activity layout I don't have anything

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.irixgalicia.irixhealth.app.comidaDiaria.AnadirAlimentoActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_anadir_alimento" />

And these is the code of the activity where I inflate

    @Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_anadir_alimento, menu);
    final MenuItem menuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(this);

    final MenuItem contadorItem = menu.findItem(R.id.alimentos_basket);
    contadorItem.setIcon(buildCounterDrawable(contadorAlimentos, R.drawable.ic_food_variant_white_36dp));

    return super.onCreateOptionsMenu(menu);
}

In my layout I only have the toolbar, not the icons inside.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

Has anyone knows how to achieve what I'm looking for..

Thank you!


回答1:


Use ifRoom|collapseActionView for the attribute showAsAction

<item
        android:id="@+id/action_search"
        android:title="search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_search"
        app:showAsAction="ifRoom|collapseActionView"/>



回答2:


Change the showAsAction attribute to ifRoom and try -

<item
  android:id="@+id/alimentos_basket"
  android:orderInCategory="1"
  android:title="@string/alimentos_anadidos"
  app:showAsAction="ifRoom" />


来源:https://stackoverflow.com/questions/36976163/push-icons-away-when-expandig-searchview-in-android-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!