Android SearchView是否永久向上移动AppBar?

Android SearchView是否永久向上移动AppBar?,android,searchview,android-appbarlayout,appbar,Android,Searchview,Android Appbarlayout,Appbar,我正在运行时将SearchView添加到当前视图中,如下所示: View searchpage = inflater.inflate(R.layout.search_ui, (ViewGroup) view.getParent(), false); SearchView searchbar = (SearchView) searchpage.findViewById(R.id.searchbar); //ViewGroup of current layout parent

我正在运行时将SearchView添加到当前视图中,如下所示:

View searchpage = inflater.inflate(R.layout.search_ui, (ViewGroup) view.getParent(), false);
    SearchView searchbar = (SearchView) searchpage.findViewById(R.id.searchbar);
    //ViewGroup of current layout
    parent.addView(searchbar);
此代码在按下按钮时执行

此代码有效,但是如果输入和退出SearchView,则向上移动AppBar的动画有效,但退出AppBar时不会重置。SearchView没有嵌入到AppBar中,我也没有尝试这样做。我希望SearchView位于AppBar的下方

AppBar在单独的xml文件中定义。我使用ViewPager在多个视图之间切换

以下是一些屏幕截图:

搜索栏是用xml定义的,如下所示:

<android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchbar"
        android:layout_below="@+id/view"
        android:iconifiedByDefault="false"
        android:queryHint="Search"
        android:layout_centerHorizontal="true">

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

AppBar的定义如下:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

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

任何帮助都将不胜感激,我不知道为什么AppBar不会自动滑回

编辑:我同时遇到一组按钮没有出现在屏幕底部的问题。我发现他们被画在导航栏下面。我相信当进入SearchView时,工具栏被向上推,以适应屏幕绝对底部的一组按钮。我在布局中添加了填充,我相信这就是问题的解决方案

谢谢


-n、 帕克

试试这样的东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical"
tools:context="Your-Activity-name-like-com.xyz.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.SearchView
    android:id="@+id/action_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:title="@string/action_search">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"/>

在“layout”文件夹中,创建一个toolbar.xml,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical"
tools:context="Your-Activity-name-like-com.xyz.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.SearchView
    android:id="@+id/action_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:title="@string/action_search">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"/>


现在您也可以在其他活动中重用此工具栏,如有必要

试试这样的东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical"
tools:context="Your-Activity-name-like-com.xyz.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.SearchView
    android:id="@+id/action_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:title="@string/action_search">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"/>

在“layout”文件夹中,创建一个toolbar.xml,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical"
tools:context="Your-Activity-name-like-com.xyz.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.SearchView
    android:id="@+id/action_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:title="@string/action_search">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"/>


现在您也可以在其他活动中重用此工具栏,如有必要

在您的
AndroidManifest
中,您是否设置了?(如果是,您是否在使用
adjustPan
?)我没有做这两件事,是吗?我不会。我在问,以防这可能是原因。在您的
AndroidManifest
中,您是否设置了一个?(如果是,您是否在使用
adjustPan
?)我没有做这两件事,是吗?我不会。我在问,以防这可能是原因。我不知道这将如何解决我的问题。我正在从它自己的xml文件加载SearchView,并将其添加到已加载的视图中。我应该在工具栏上做类似的事情吗?你问“我希望SearchView位于AppBar的下方。”。此外,设计建议您从一个文件导入工具栏,而不是创建单个工具栏。我不知道这将如何解决我的问题。我正在从它自己的xml文件加载SearchView,并将其添加到已加载的视图中。我应该在工具栏上做类似的事情吗?你问“我希望SearchView位于AppBar的下方。”。此外,设计建议您从一个文件导入工具栏,而不是创建单个工具栏。