Tôi đã tìm ra giải pháp để mở rộng hoàn toàn chế độ xem tìm kiếm theo chiều ngang và cũng có chế độ xem hành động đã được mở rộng khi hoạt động được tạo. Dưới đây là cách hoạt động:
1.Đầu tiên tạo tệp xml trong thư mục trình đơn của bạn được gọi là ví dụ: searchview_in_menu.xml. Ở đây bạn sẽ có đoạn mã sau:
<?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>
Lưu ý: "@ string/tìm kiếm" - trông giống như thế này trong res-strings.xml:
<string name="search">Search</string>
2.Second tạo bố trí gọi ở trên ("@ layout/searchview_layout") trong thư mục bố cục lại. Cách bố trí mới: searchview_layout.xml sẽ trông như thế này:
<?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"/>
Lưu ý: Ở đây chúng ta đang thiết lập chiều rộng view tìm kiếm để phù hợp với chiều rộng của mẹ (android: layout_width = "match_parent")
3 .in lớp MainActivity của bạn hoặc trong các hoạt động mà đã thực hiện Kiện Tìm Kiếm Xem ghi trong onCreateOptionsMenu() phương pháp đoạn mã sau:
@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. Tôi cần phải sử dụng 'setOnSearchClickListener() 'vì tôi sẽ không tìm kiếm báo chí phần cứng. –