如何在 android 中显示长搜索栏
How to Show long search bar in android
我想在工具栏中创建一个搜索栏,例如 this.Search 带有 EditText 框的图标显示
EditText
的xml里面有一个属性
android:drawableLeft="@drawable/yoursearchIcon"
您可以在 EditText 的 xml 中使用它和您想要搜索的图标。要使用默认搜索图标,请使用 android.drawable...
访问它
声明一个菜单项:-
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
在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);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
我想在工具栏中创建一个搜索栏,例如 this.Search 带有 EditText 框的图标显示
EditText
的xml里面有一个属性android:drawableLeft="@drawable/yoursearchIcon"
您可以在 EditText 的 xml 中使用它和您想要搜索的图标。要使用默认搜索图标,请使用 android.drawable...
访问它声明一个菜单项:-
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
在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);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}