【发布时间】: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