Android – SearchView在工具栏中不展开

我试图在我的工具栏添加一个SearchView,我已经添加了图标,但是当我按下它时,它不会扩展。

代码如下:

main_menu.xml

<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.app.try.MainActivity"> <item android:id="@+id/search" android:title="@string/search" android:icon="@drawable/abc_ic_search_api_mtrl_alpha" app:showAsAction="always" android:actionViewClass="android.support.v7.widget.SearchView"/> </menu> 

MainActivity.java

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.menu_main,menu); MenuItem searchItem = menu.findItem(R.id.search); SearchManager searchManager= (SearchManager)getSystemService(Context.SEARCH_SERVICE); SearchView searchView=null; if (searchItem!=null) { Log.d("createOptionMenu","search item not null"); searchView = (SearchView) searchItem.getActionView(); } if (searchView!=null) { Log.d("createOptionMenu","search view not null"); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); }else{ Log.e("createOptionMenu","search view null"); } return true; } 

(这总是显示日志消息“search view null”)

AndroidManifest.xml中

 ... <activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name" android:screenOrientation="portrait" > <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> <activity android:name=".SearchResultActivity" android:label="@string/title_activity_search_result" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> ... 

searchable.xml

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="HINTTT" /> 

我已经尝试了使用expandActionView()setIconified(false)多个选项,但不是那些为我工作。

Solutions Collecting From Web of "Android – SearchView在工具栏中不展开"

你应该使用MenuItemCompat

 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); 

并用于Collapse SearchView的使用

 MenuItemCompat.collapseActionView(searchItem); 

并确保您已将android:actionViewClass更改为app:actionViewClass

我有同样的问题。 我刚刚通过一个小调整来解决我的问题。 改变android:actionViewClass="android.support.v7.widget.SearchView"

app:actionViewClass="android.support.v7.widget.SearchView"

在MainActivity中,你必须扩展ActionbarActivity

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:appcompat="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/tools"> <item android:id="@+id/menu_search" android:title="@string/menu_search" appcompat:actionViewClass="android.support.v7.widget.SearchView" appcompat:showAsAction="always"/> 

MainActivity.java

 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); // Inflate menu to add items to action bar if it is present. inflater.inflate(R.menu.menu_main, menu); // Associate searchable configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); return true; } 

}