【问题标题】:How can I put a SearchView inside of a ToolBar with a height set to wrap_content without it disappearing?如何将 SearchView 放置在高度设置为 wrap_content 的 ToolBar 内而不消失?
【发布时间】:2016-11-17 15:08:27
【问题描述】:

我注意到,如果我将 android.support.v7.widget.SearchView 放置在 android.support.v7.widget.Toolbar 内部,并将其高度设置为 wrap_content,则 SearchView 的行为会非常奇怪。起初它看起来很好,但一旦它获得焦点,它就会消失并且不再占用布局中的空间。这可以通过为Toolbar 设置显式高度来解决,但我正在尝试使用wrap_content,特别是因为我的用例需要Toolbar 动态调整大小。

还可能值得一提的是,SearchView 在消失后仍然可以使用。如果我将SearchView.OnQueryTextListener 添加到SearchView,那么我可以观察到查询文本在我输入时确实在更新。这段文字和SearchView 的任何其他部分都不会占用布局中的空间,如果你强制它在布局中不被遮挡,那么它仍然不可见。

在我看来,这种情况就像 SearchView 中的一个错误。是否有人对它发生的原因或如何解决它有更深入的了解?

以下是该问题的一个工作示例:

<?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.me.MyActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <android.support.v7.widget.SearchView
                android:id="@+id/internalSearchView"
                app:queryHint="To..."
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </android.support.v7.widget.SearchView>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 你不能将匹配父级设置为高度,它会匹配工具栏高度
  • 在这个简单的例子中可以正常工作,但我希望它是wrap_content,因为我实际上有额外的动态视图可以改变有效高度并需要Toolbar 展开。在没有额外视图的情况下可以观察到错误行为,因此我将它们从示例代码中删除。

标签: android android-layout android-toolbar searchview


【解决方案1】:

它看起来应该可以工作...我有两个建议。

  1. 如果它不影响您的设计目标,请在您的工具栏上设置android:minHeight="?attr/actionBarSize"。无论如何,这可能是需要的。
  2. 尝试在 SearchView 上设置 android:layout_height="48dp"。

【讨论】:

  • 添加minHeight 无效,但明确指定layout_height 有效!感谢您的解决方法!
【解决方案2】:

不不不... 使用这些参数设置搜索视图:

<FrameLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay"
                android:animateLayoutChanges="true">

            <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

            <com.lapism.searchview.widget.SearchView
                    android:id="@+id/searchView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/search_behavior"
                    app:search_version="menu_item"
                    app:search_version_margins="menu_item"/>
        </FrameLayout>

【讨论】:

    猜你喜欢
    • 2015-11-12
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多