Android 如何将SearchView放置在工具栏中,并将其高度设置为“包装”内容,而不使其消失?
我注意到,如果我将Android 如何将SearchView放置在工具栏中,并将其高度设置为“包装”内容,而不使其消失?,android,android-layout,android-toolbar,searchview,Android,Android Layout,Android Toolbar,Searchview,我注意到,如果我将android.support.v7.widget.SearchView放在android.support.v7.widget.Toolbar中,高度设置为wrap\u content时,SearchView的行为非常奇怪。它一开始看起来很好,但一旦获得焦点,它就会消失,不再占用布局中的空间。这可以通过为工具栏设置一个显式高度来解决,但我正试图使用包装内容,因为我的用例需要工具栏动态调整大小 还值得一提的是,SearchView在消失后仍能正常工作。如果我将SearchView
android.support.v7.widget.SearchView
放在android.support.v7.widget.Toolbar
中,高度设置为wrap\u content
时,SearchView
的行为非常奇怪。它一开始看起来很好,但一旦获得焦点,它就会消失,不再占用布局中的空间。这可以通过为工具栏
设置一个显式高度来解决,但我正试图使用包装内容
,因为我的用例需要工具栏
动态调整大小
还值得一提的是,SearchView
在消失后仍能正常工作。如果我将SearchView.OnQueryTextListener
添加到SearchView
中,那么我可以观察到查询文本确实在我键入时被更新。但是,此文本或SearchView
的任何其他部分都不会占用版面中的空间,如果您强制它在版面中不被遮挡,则它仍然不可见
在我看来,这似乎是SearchView
中的一个bug。有没有人能更深入地了解它为什么会发生,或者如何解决它
以下是该问题的一个工作示例:
<?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”
。无论如何,这可能是需要的李>
<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>
y无法将match parent设置为高度,它将与工具栏高度匹配,这在这个简单的示例中可以很好地工作,但我希望它是wrap\u content
,因为我实际上在其中有其他动态视图,可以更改有效高度,并需要工具栏
展开。在没有额外视图的情况下可以观察到buggy行为,因此我将它们从示例代码中删除。添加minHeight
没有效果,但明确指定layout\u height
有效!谢谢你的解决方案!