展开搜索视图时,如何构建ActionBar
具有可见单个操作项的可折叠搜索视图?为了更具描述性,这就是我需要的:
请注意,还有其他菜单项并
android:uiOptions="splitActionBarWhenNarrow"
在 中定义AndroidManifest.xml
。
我尝试设置自定义搜索项布局:
菜单.xml
<item
android:id="@+id/menu_search"
android:actionLayout="@layout/actionbar_search"
android:icon="@drawable/action_search"
android:showAsAction="always|collapseActionView"
android:title="@string/search" />
actionbar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal" >
<com.actionbarsherlock.widget.SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:iconifiedByDefault="false"
android:queryHint="@string/search_hint" />
<ImageButton
android:id="@+id/add_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="@style/Widget.Sherlock.ActionButton"
android:src="@drawable/content_new"/>
</LinearLayout>
但默认情况下,搜索视图采用所有可用宽度并且按钮不可见。我不知道如何强制SearchView
填充应用程序图标和菜单项之间的所有可用空间。我发现的只是android:maxWidth
属性,但这只允许硬编码维度,我正在寻找一些更灵活的解决方案。我也尝试过RelativeLayout
,android:layout_toRightOf="@id/search_view"
但没有运气。