当搜索视图处于活动状态时,菜单项不会隐藏 android
我在片段的工具栏中有一个搜索视图,但是当我单击搜索项时,我希望其他菜单项隐藏,并且搜索视图占据整个宽度。但是当单击搜索视图时,菜单项不会隐藏,这非常不方便。我尝试使用 searchView.setMaxWidth(Integer.MAX_VALUE);
但它不起作用。
toolbar.inflateMenu(R.menu.files_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
SearchView searchView = (SearchView) item.getActionView();
searchView.setQueryHint("Search");
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.grey));
} else {
toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.light_red));
toolbar.collapseActionView();
}
这是 files_menu
<?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/search"
android:title="search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>
<item
android:id="@+id/go"
android:icon="@drawable/ic_baseline_go_24"
app:showAsAction="ifRoom|collapseActionView"
android:title="go"/>
<item
android:icon="@drawable/ic_baseline_more_vert_white"
android:id="@+id/menu_overflow"
app:showAsAction="ifRoom|collapseActionView"
android:title="TODO">
<menu>
<item
android:title="Find"
android:id="@+id/find"
/>
</menu>
</item>
</menu>
我还尝试在菜单 xml 中定义操作布局,但效果不佳。
<item android:id="@+id/search"
android:title="search"
android:icon="@drawable/ic_search"
android:actionLayout="@layout/searchview"
app:showAsAction="ifRoom|collapseActionView"
/>
布局/searchview.xml 是
<androidx.appcompat.widget.SearchView
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="wrap_content"
android:maxWidth="10000dp" />
I have a searchview in toolbar in a fragment but when I click on the search item I want the other menu items to hide and searchview to take the full width. But the menu items don't hide when the searchview is clicked which is very inconvenient. I tried to use searchView.setMaxWidth(Integer.MAX_VALUE);
but it didn't work.
toolbar.inflateMenu(R.menu.files_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
SearchView searchView = (SearchView) item.getActionView();
searchView.setQueryHint("Search");
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.grey));
} else {
toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.light_red));
toolbar.collapseActionView();
}
This the files_menu
<?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/search"
android:title="search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>
<item
android:id="@+id/go"
android:icon="@drawable/ic_baseline_go_24"
app:showAsAction="ifRoom|collapseActionView"
android:title="go"/>
<item
android:icon="@drawable/ic_baseline_more_vert_white"
android:id="@+id/menu_overflow"
app:showAsAction="ifRoom|collapseActionView"
android:title="TODO">
<menu>
<item
android:title="Find"
android:id="@+id/find"
/>
</menu>
</item>
</menu>
I also tried to define the action layout in menu xml but that did not work as well.
<item android:id="@+id/search"
android:title="search"
android:icon="@drawable/ic_search"
android:actionLayout="@layout/searchview"
app:showAsAction="ifRoom|collapseActionView"
/>
layout/searchview.xml is
<androidx.appcompat.widget.SearchView
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="wrap_content"
android:maxWidth="10000dp" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是重复的问题,你可以检查以下问题和答案
展开搜索视图以使用整个操作栏(隐藏其他事情)
I think it is duplicated question, you can check the following questions and the answers
Expand Search View to use entire Action Bar (hide other things)