我在menu.xml中实现了android.support.v7.widget.SearchView,只有当我使用工具栏而不是操作栏时才能正常工作。当我使用操作栏时,布局会将此小部件的X按钮稍微向左移动,在按钮和屏幕右侧之间留下空白区域。当我使用工具栏时,这不会发生。请查看截图并告诉我我能做些什么。
我正在使用股票android Marshmallow。
这是menu_main.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.appsbyusman.ppc.MainActivity">
<item
android:id="@+id/action_search"
android:title="@android:string/search_go"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:visible="true"/>
</menu>
这是相关的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setQueryRefinementEnabled(true);
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
mSearchViewAdapter = new SearchFeedResultsAdaptor(MainActivity.this, R.layout.search_feed_list_item, null, columns, null, -1000);
searchView.setSuggestionsAdapter(mSearchViewAdapter);
return true;
}
这是活动xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.appsbyusman.ppc.MainActivity">
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_search" />
</android.support.design.widget.CoordinatorLayout>