如何将 SearchView 放入高度设置为 wrap_content 的 ToolBar 中而不消失?
How can I put a SearchView inside of a ToolBar with a height set to wrap_content without it disappearing?
我注意到,如果我将一个 android.support.v7.widget.SearchView
放在一个 android.support.v7.widget.Toolbar
中,并将其高度设置为 wrap_content
,那么 SearchView
的行为会非常奇怪。一开始看起来不错,但一旦获得焦点,它就会消失,不再占用布局中的 space。这可以通过为 Toolbar
设置一个显式高度来解决,但我正在尝试使用 wrap_content
具体因为我的用例需要 Toolbar
动态调整大小。
可能还值得一提的是,SearchView
在消失后仍然可以使用。如果我将 SearchView.OnQueryTextListener
添加到 SearchView
然后我可以观察到查询文本确实在我键入时更新。但是,此文本和 SearchView
的任何其他部分都不会在布局中占据 space,如果您强制它在布局中不被遮挡,那么它仍然不可见。
对我来说,这似乎是 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>
它确实看起来应该有效...我有两个建议。
- 如果它不影响您的设计目标,请在您的工具栏上设置
android:minHeight="?attr/actionBarSize"
。无论如何,这可能是需要的。
- 尝试在 SearchView 上设置
android:layout_height="48dp"
。
不不不...
使用这些参数设置搜索视图:
<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>
我注意到,如果我将一个 android.support.v7.widget.SearchView
放在一个 android.support.v7.widget.Toolbar
中,并将其高度设置为 wrap_content
,那么 SearchView
的行为会非常奇怪。一开始看起来不错,但一旦获得焦点,它就会消失,不再占用布局中的 space。这可以通过为 Toolbar
设置一个显式高度来解决,但我正在尝试使用 wrap_content
具体因为我的用例需要 Toolbar
动态调整大小。
可能还值得一提的是,SearchView
在消失后仍然可以使用。如果我将 SearchView.OnQueryTextListener
添加到 SearchView
然后我可以观察到查询文本确实在我键入时更新。但是,此文本和 SearchView
的任何其他部分都不会在布局中占据 space,如果您强制它在布局中不被遮挡,那么它仍然不可见。
对我来说,这似乎是 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>
它确实看起来应该有效...我有两个建议。
- 如果它不影响您的设计目标,请在您的工具栏上设置
android:minHeight="?attr/actionBarSize"
。无论如何,这可能是需要的。 - 尝试在 SearchView 上设置
android:layout_height="48dp"
。
不不不... 使用这些参数设置搜索视图:
<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>