我想在工具栏菜单中设置自定义搜索布局。我的问题是当我将扩展监听器设置为搜索项时 - 操作视图不再扩展 我试着打电话给
-
item.expandActionView();
MenuItemCompat.expandActionView(item);
的 search_layout.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:actionLayout="@layout/search_layout"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:showAsAction="always|collapseActionView"/>
</menu>
内部片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/closeSearchImageView"
android:layout_toStartOf="@+id/closeSearchImageView"
android:background="@null"
android:hint="@string/search"
android:imeOptions="flagNoExtractUi"
android:inputType="textCapSentences"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="@android:color/white"
android:textColorHint="#7dffffff"
android:textCursorDrawable="@drawable/cursor"/>
<ImageView
android:id="@+id/closeSearchImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="3dp"
android:src="@drawable/abc_ic_clear_mtrl_alpha"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="@+id/searchEditText"
android:background="#7dffffff"/>
</RelativeLayout>
任何想法?
答案 0 :(得分:0)
如果其他人后来发现这个问题(比如我),问题就在这里:
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
通过返回false覆盖菜单项的原始操作。这应该是:
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
//what you want to do
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//what you want to do
return true;
}