如何让searchview填充剩余的空间?

时间:2018-05-28 13:51:22

标签: android android-layout searchview

我想在我的应用操作栏中显示两个图标和一个搜索视图。我使用支持ToolbarNoActionBar主题。我试图将一个图标放在左侧,一个放在右侧,然后将SearchView放在两者之间,取出其间的所有剩余空间。

应用于SearchView的自定义设置:

  • app:iconifiedByDefault="false"(以便默认情况下展开视图)
  • app:searchIcon="@drawable/search_action_bar_icon"(搜索图标)

我已经尝试过:

1。没有应用样式:

在这种情况下,搜索视图不够拉伸,因为它默认设置了最大宽度。

The appearance of the action bar, first attempt

2。最大宽度增加

然后,我将最大宽度增加到一个非常高的值(10000dp),这样它就可以自由扩展。将宽度设置为match_parentwrap_content后,搜索视图会将所有剩余空间设置为右侧,因此第二个图标会显示在屏幕外。

The appearance of the action bar, first attempt

第3。使用layout_weigth

使用布局检查器,我发现包含菜单项的视图组是ActionMenuView,其超类是LinearLayoutCompat。在LinearLayout中实现所需结果的建议解决方案是将中间孩子的宽度设置为0dp,将layout_weight设置为1,而其他孩子保持固定宽度。但是,SearchView只出现了0dp宽,这可能意味着ActionMenuView的宽度设置为wrap_content - 我不确定这是不是诚实。< / p>

The appearance of the action bar, third attempt

那么,如何做到这一点:

The desired outcome

1 个答案:

答案 0 :(得分:0)

试试这个

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="10dp"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/ic_launcher" />

    <android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/ic_launcher" />
</LinearLayout>