gpt4 book ai didi

android - 其他选项菜单项在展开的搜索 View 折叠后不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:19 26 4
gpt4 key购买 nike

我有一个带有选项菜单设置的简单 Activity ,代码如下所示。我遇到的问题是,在展开和折叠搜索 View 后,带有 app:showAsAction="ifRoom" 的菜单项不会出现,即使工具栏上有空间。另一方面,带有 app:showAsAction="never" 的菜单项会出现。

menu_simple_activity.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/action_search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_filter_list_white_24dp"
android:title="@string/action_filter"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_sort"
android:icon="@drawable/ic_sort_white_24dp"
android:title="@string/action_sort"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_print_all"
android:icon="@drawable/ic_print_white_24dp"
android:title="@string/action_print_all"
app:showAsAction="never" />
</menu>

SimpleActivity.java

public class SimpleActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_simple_activity, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
final MenuItem filterMenuItem = menu.findItem(R.id.action_filter);
final MenuItem sortMenuItem = menu.findItem(R.id.action_sort);
final MenuItem printAllMenuItem =
menu.findItem(R.id.action_print_all);

searchMenuItem.setOnActionExpandListener(new
MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
filterMenuItem.setVisible(false);
sortMenuItem.setVisible(false);
printAllMenuItem.setVisible(false);
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
filterMenuItem.setVisible(true);
sortMenuItem.setVisible(true);
printAllMenuItem.setVisible(true);
return true;
}
});
return true;
}
}

我该如何解决?

最佳答案

你应该调用invalidateOptionsMenu()将项目设置为可见后。

来自文档:

Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed.

关于android - 其他选项菜单项在展开的搜索 View 折叠后不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47535227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com