折叠时,我的搜索视图停留在右侧,取代了工具栏的标题。
菜单文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@android:string/search_go"
app:actionLayout="@layout/full_width_search_view"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="@+id/btn_create"
android:icon="@drawable/ic_add_white_24dp"
android:title="@string/create"
app:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="@+id/btn_delete"
android:icon="@drawable/ic_delete_white_24dp"
android:title="@string/remove"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
全宽搜索视图(显然不起作用......):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
活动代码:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_create_delete, menu)
// Handle Searching
val searchView: SearchView = menu.findItem(R.id.search).actionView as SearchView
searchView.isIconified = false
searchView.clearFocus()
menu.findItem(R.id.btn_delete).isVisible = orderLine.hasCustomer()
return super.onCreateOptionsMenu(menu)
}
感谢任何帮助。提前谢谢!
答案 0 :(得分:0)
您必须覆盖onBackPressed()
方法,因为您可以通过按ActionBar中的后退按钮或后退按钮来折叠SearchView
如果按后退按钮,则:
override fun onBackPressed() {
if (!searchView.isIconified) {
searchView.isIconified == true;
} else {
super.onBackPressed();
}
}
如果按ActionBar后退按钮,则必须覆盖onOptionsItemSelected()方法,然后必须按照上面给出的相同方式进行操作。
答案 1 :(得分:0)
我已经通过删除collapseActionView
和searchView.isIconified = false
菜单文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@android:string/search_go"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/btn_create"
android:icon="@drawable/ic_add_white_24dp"
android:title="@string/create"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/btn_delete"
android:icon="@drawable/ic_delete_white_24dp"
android:title="@string/remove"
app:showAsAction="ifRoom"/>
</menu>
活动代码:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_create_delete, menu)
// Handle Searching
val searchView: SearchView = menu.findItem(R.id.search).actionView as SearchView
searchView.queryHint = "Search Partner..."
// ...
return super.onCreateOptionsMenu(menu)
}