如何添加button在Android工具栏search视图?

现在我有下一个代码:

@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_toolbar_reader, 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); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { return false; } }); return true; } 

我想附加search查看buttonsearch下一个和search以前(在WebView中search)。 例如我想得到这个(绘制的箭头应该只在searchView uncollapsed时出现): 例

我怎样才能做到这一点?

Solutions Collecting From Web of "如何添加button在Android工具栏search视图?"

所以,我通过这个代码在我的活动中解决了它:

 @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_toolbar_reader, menu); final MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) searchItem.getActionView(); LayoutParams navButtonsParams = new LayoutParams(toolbar.getHeight() * 2 / 3, toolbar.getHeight() * 2 / 3); Button btnNext = new Button(this); btnNext.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_down_white_48pt_3x)); Button btnPrev = new Button(this); btnPrev.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_up_white_48pt_3x)); searchStats = new TextView(this); ((LinearLayout) searchView.getChildAt(0)).addView(searchStats); ((LinearLayout) searchView.getChildAt(0)).addView(btnPrev, navButtonsParams); ((LinearLayout) searchView.getChildAt(0)).addView(btnNext, navButtonsParams); ((LinearLayout) searchView.getChildAt(0)).setGravity(Gravity.BOTTOM); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { searchQuery = newText; if (!newText.equals("")) { lawTextWebView.findAllAsync(newText); } else { lawTextWebView.clearMatches(); } return false; } }); btnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!searchQuery.equals("")) { lawTextWebView.findNext(true); } } }); btnPrev.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!searchQuery.equals("")) { lawTextWebView.findNext(false); } } }); return true; } 

并保存此操作栏标记res / menu / menu_toolbar_reader.xml:

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_search" android:title="@android:string/search_go" android:icon="@drawable/abc_ic_search_api_mtrl_alpha" yourapp:showAsAction="always|collapseActionView" yourapp:actionViewClass="android.support.v7.widget.SearchView" /> <item android:id="@+id/action_contents" android:title="@string/contents" android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha" yourapp:showAsAction="always" /> </menu>