Udało mi się znaleźć rozwiązanie umożliwiające pełne rozwinięcie widoku wyszukiwania w widoku poziomym oraz rozszerzenie widoku akcji już po utworzeniu działania. Oto, jak to działa:
1. Najpierw utwórz plik xml w folderze res-menu, na przykład: searchview_in_menu.xml. Tutaj można mieć następujący kod:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:title="@string/search"
android:icon="@android:drawable/ic_menu_search"
android:actionLayout="@layout/searchview_layout" />
</menu>
Uwaga: "@ ciąg/search" - wygląda coś takiego w RES-strings.xml:
<string name="search">Search</string>
2.Second stworzyć układ, o którym mowa powyżej ("@ layout/searchview_layout") w folderze res-layout. Nowy układ: searchview_layout.xml będzie wyglądać następująco:
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Uwaga: Tu ustawiasz szerokość widoku wyszukiwania, aby dopasować szerokość jego rodzica (Android: layout_width = „match_parent”)
3 .W klasie główną działalność lub czynności, które musi realizować Szukaj Zobacz zapis w onCreateOptionsMenu() metoda następujący kod:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
//find the search view item and inflate it in the menu layout
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
//set a hint on the search view (optional)
mSearchView.setQueryHint(getString(R.string.search));
//these flags together with the search view layout expand the search view in the landscape mode
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_ALWAYS);
//expand the search view when entering the activity(optional)
searchItem.expandActionView();
return true;
}
Close. Zlikwidowałem potrzebę użycia 'setOnSearchClickListener()', ponieważ nie będę szukał wyszukiwania za pomocą klucza sprzętowego. –