材料搜索栏未显示

时间:2016-09-17 20:21:28

标签: java android xml search

出于某种原因,我无法将材料搜索栏显示在我的代码中。我想让它出现在NewImageActivity的负载上,然后允许用户输入搜索词。然后,该术语将用于填充GridView:

menu_main.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=".NewImageActivity">
<item android:id="@+id/action_search"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

活动

public class NewImageActivity extends AppCompatActivity {

private SearchView mSearchView;
private RecyclerView mImageResults;
private RecyclerView.Adapter mImageResultsAdapter;
private ArrayList<Flicker> mPhotoArray;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_image);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    // Retrieve the SearchView and plug it into SearchManager
    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;
}

0 个答案:

没有答案