【发布时间】:2013-03-03 13:59:43
【问题描述】:
当搜索视图展开时,如何使用可折叠的搜索视图构建ActionBar,并显示单个操作项?为了更具描述性,这就是我需要的:
请注意,还有其他菜单项,android:uiOptions="splitActionBarWhenNarrow" 定义在 AndroidManifest.xml 中。
我尝试设置自定义搜索项布局:
menu.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",但没有成功。
【问题讨论】:
标签: android android-actionbar searchview