我有一个带有ViewPager的MainActivity,并将FirstFragment添加到此View Pager,并将另一个名为SecondFragment的片段添加到FirstFragment。在SecondFragment中,我通过以下代码在ActionBar中创建菜单: filter_menu.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_sort"
android:icon="@drawable/ic_sort"
android:orderInCategory="300"
android:title="sort"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_filter"
android:orderInCategory="300"
android:title="Filter"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search_grey_24dp"
android:title="Search"
app:actionLayout="@layout/search_view_layout"
android:visible="false"
app:showAsAction="always|collapseActionView" />
</menu>
search_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:theme="@style/SearchViewTheme"
android:iconifiedByDefault="true"
app:queryHint="@string/type_your_text" />
在我的片段中:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.filter_menu, menu);
searchItem = menu.findItem(R.id.action_search);
sortItem = menu.findItem(R.id.action_sort);
filterItem = menu.findItem(R.id.action_filter);
searchView = (SearchView) searchItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
super.onCreateOptionsMenu(menu, inflater);
}
我不想在ActionBar中显示SearchView的图标。我想在单击菜单项 action_filter 并隐藏其他菜单项时展开SearchView;我有两个问题。一种是如何显示扩展的SearchView的后退图标(左V形),另一种是SearchView没有填充ActionBar的宽度。我该如何解决这两个问题?