大家好我刚接触这个新手到android开发,我试图修改这个项目Click HERE
我想在操作栏中使用搜索小部件过滤城市,但是我已设法将其添加到操作栏中,但我不知道是谁让它过滤引用名称。
这是我的menu.xml代码
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="Search"/>
AndroidManifiest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vatsag.samples.weatherdisplay"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".WeatherActivity"
android:label="@string/title_activity_weather" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".SampleActivity"
android:label="@string/title_activity_weather">
</activity>
</application>
答案 0 :(得分:0)
在您的活动中尝试此操作:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(null!=searchManager ) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setIconifiedByDefault(false);
return true;
}