问题描述
我正在使用Android SearchView在我的操作栏中设置搜索按钮。我在YouTube上关注了Google开发者的教程,并添加了一些基于我的调整自己的需要。但是,每当我退出单击按钮时出现的搜索字段时,搜索按钮就会消失!
I'm using android SearchView to have a search button in my action bar. I followed this tutorial from Google Developers on YouTube, and added a few tweaks based on my own needs. However, whenever I exit the search field that comes up when clicking the button, the search button disappears!
这是我的onCreateOptionsMenu和onOptionsItemSelected函数
Here is my onCreateOptionsMenu and onOptionsItemSelected functions
onCreateOptionsMenu:
onCreateOptionsMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
onOptionsItemSelected:
onOptionsItemSelected:
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// TODO: settings activity
break;
case R.id.action_logout:
FirebaseAuth.getInstance().signOut();
break;
case R.id.action_search:
// Initialize everything here, because it produces a NullPointerException if initialized in onCreateOptionsMenu
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
break;
}
return super.onOptionsItemSelected(item);
}
这是我的menu_main.xml布局文件
And here's my menu_main.xml layout file
<menu 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"
tools:context="com.magnus.inline.MainActivity">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_logout"
android:orderInCategory="200"
android:title="@string/action_logout"
app:showAsAction="never" />
和searchable.xml布局文件
And searchable.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</searchable>
编辑:
我注意到搜索按钮消失的唯一时间是在搜索模式下按下选项按钮时(参见链接中的最后一张图像)。我有一种感觉,当显示选项菜单时,action_search以某种方式转换为选项菜单项,而不是操作栏项(?)。对于任何人来说,每当我尝试在onCreateOptionsMenu函数中调用searchview MenuItem的MenuItemCompat.getActionView()时,我的应用程序就会崩溃并说它试图在空对象引用上执行它。
I noticed that the only time the search button disappears is when I've pressed the options button while in "search mode"(see the last image in link). I have a feeling that when the options menu is displayed, the "action_search" is somehow converted to a options menu item, instead of an action bar item (?). For anyone asking, whenever I try to call MenuItemCompat.getActionView() of a searchview MenuItem in the onCreateOptionsMenu function, my app just crashes and says that it tries to perform it on a null object reference.
这是我的activity_main.xml布局文件
Here's my activity_main.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.magnus.inline.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/main_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AlertDialog.AppCompat.Light"
android:background="@color/colorPrimaryDark" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
推荐答案
而不是
app:showAsAction="ifRoom|collapseActionView"
更改为
app:showAsAction="always|collapseActionView
这篇关于按下后退按钮/折叠搜索字段时,Android搜索菜单项会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!