У меня есть макет, где я использую пользовательскую панель инструментов со значком и центрированным текстом.
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 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"
style="@style/ToolbarThemeMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/toolbarStyle"
app:titleMarginStart="@dimen/default_screen_margin_normal"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/toolbarTvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_gravity="center"
tools:text="@string/app_name"
android:textColor="@color/white"
android:textSize="@dimen/toolbar_title_size"
android:layout_marginRight="?android:attr/actionBarSize"
android:layout_marginLeft="?android:attr/actionBarSize"
/>
<ImageView
android:id="@+id/toolbarIv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:src="@drawable/icon"
android:paddingStart="@dimen/default_screen_margin_normal"
/>
</androidx.appcompat.widget.Toolbar>
Я добавляю эту панель инструментов к XML следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgColor"
android:orientation="vertical">
<include
android:id="@+id/activityMainToolbar"
layout="@layout/toolbar"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/activityMainVp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/activityMainBnv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:labelVisibilityMode="unlabeled"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>
Здесь это мое menu_explore. 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/menu_explore_search"
android:icon="@drawable/ic_search_white"
android:title="@string/search"
app:showAsAction="ifRoom|withText"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
<item
android:id="@+id/menu_explore_date"
android:title="@string/date"
android:icon="@drawable/ic_calendar_white"
app:showAsAction="always" />
</menu>
Находясь в ExploreFragment, я имею следующее:
@Override public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
this.menu = menu;
inflater.inflate(R.menu.menu_explore, menu);
showCalendarIcon();
MenuItem menuSearch = menu.findItem(R.id.menu_explore_search);
menuSearch.collapseActionView();
SearchView searchView = (SearchView) menuSearch.getActionView();
searchView.setQueryHint(getString(R.string.search));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override public boolean onQueryTextSubmit(String query) {
params.client = query;
params.page = 1;
presenter.getExploreEvents(params);
return false;
}
@Override public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnCloseListener(() -> {
params.page = 1;
params.client = null;
presenter.getExploreEvents(params);
return false;
});
}
@Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_explore_date:
showDateFilterDialog();
break;
}
return super.onOptionsItemSelected(item);
}
But I get expanded and too big toolbar when I press search.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/zR2bd.png