Tôi đang cố gắng thêm một SearchView trong thanh công cụ của mình, tôi đã thêm biểu tượng, nhưng khi tôi nhấn nó, nó sẽ không mở rộng.Android - SearchView trên thanh công cụ không mở rộng
Dưới đây là các mã:
main_menu.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="com.app.try.MainActivity">
<item
android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager= (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView=null;
if (searchItem!=null) {
Log.d("createOptionMenu","search item not null");
searchView = (SearchView) searchItem.getActionView();
}
if (searchView!=null) {
Log.d("createOptionMenu","search view not null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}else{
Log.e("createOptionMenu","search view null");
}
return true;
}
(này luôn hiển thị thông báo đăng nhập "xem tìm kiếm null")
AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".SearchResultActivity"
android:label="@string/title_activity_search_result" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="HINTTT" />
Tôi đã thử nhiều tùy chọn như sử dụng expandActionView()
hoặc setIconified(false)
, nhưng không những tác phẩm cho tôi.
Bạn đang nhập khẩu lớp đúng. tức là android.support.v7.widget.SearchView đang hoạt động? Không bao gồm android.widget.SearchView trong hoạt động nếu bạn đang sử dụng appcompat searchVIew. – shubendrak
@Shubendra Tôi đã nhập 'andorid.widget.SearchView' thay vì' android.support.v7.widget.SearchView', nhưng tôi đã thay đổi nó và vẫn không hoạt động – Doruko