当searchview获得焦点时,Android appcompat工具栏会拉伸

ar3*_*34z 20 android searchview android-toolbar

我正在更新一个应用程序以使用新的Toolbar而不是常规的ActionBar.在我的应用中,用户必须能够从其联系人列表中选择联系人.为此,我SearchView在菜单中添加了一个.联系人已经在列表中; 该SearchView用于筛选使用列表ArrayAdapter.getFilter()方法.

这一切都很好用ActionBar,但是它Toolbar的高度被拉伸到键盘后面.使用Android设备监视器中的XML检查我可以看到ListView键盘后面的存在.

它似乎似乎SearchView想要显示建议,但我没有配置这样的东西.知道出了什么问题吗?

图像说明了这个问题.它们显示了正常,扩展和聚焦的状态SearchView.

这是我的XML菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_forwarding_contact_search"
        app:showAsAction="always"
        android:title="@string/forwarding_contact_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

编辑 给予工具栏固定的高度并不能解决问题,因为它SearchView在聚焦时会消失.改变任何一项的重力似乎都没有影响SearchView.

正常状态 膨胀的国家 聚焦状态

Rob*_*Rob 20

我遇到了与OP相同的问题,我尝试了android:fitsSystemWindows="true"解决方案,但它已经解决了一半:searchview没有扩展,但通知栏(屏幕顶部)变得完全白色(如布局背景)而不是红色(我的应用程序)主题).

我发现了一种替代方式,它的工作就像一个魅力,所以对于那些被卡住的人,试试这个:

在您的清单中,只需在您的活动部分添加以下内容:

机器人:windowSoftInputMode = "adjustPan"

例:

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustPan"
    android:label="My main activity" >
</activity>
Run Code Online (Sandbox Code Playgroud)


ar3*_*34z 19

好的,我明白了.没有问题SearchView,因为普通的EditTexts通常放在布局xml中也是如此.这Toolbar也不是问题.

我创建了一个空的活动,并在我的应用程序中改变了我所做的任何事情,最后来到了我的主题.在KitKat和后来我<item name="android:windowTranslucentStatus">true</item>设置了主题,因此导航抽屉将出现在状态栏后面.

禁用/删除此功能可解决此问题.这让我想起了这座android:fitsSystemWindows="true"房产.它设置在Toolbar,但不包含在包含的主要活动的布局xml中DrawerLayout.我猜这些DrawerLayout设置适合系统窗口.例如,这里没有fitSystemWindows房产:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DashboardActivity">
Run Code Online (Sandbox Code Playgroud)

发生问题的活动没有NavigationDrawer,这是一项新活动.android:fitsSystemWindows="true"在活动布局的根节点上设置使得工作正常.

所以,对于任何人来说:如果你有<item name="android:windowTranslucentStatus">true</item>你的主题,请确保包含工具栏的任何根节点都包含android:fitsSystemWindows="true".

工作样本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />
        </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Kis*_*ava 17

我有同样的问题,但没有帮助上面的答案,但经过大量的搜索发现了一些东西.

也可以帮到你.!!

在添加此属性后 toolbar

android:layout_height="?attr/actionBarSize"

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/myPrimaryColor"
android:minHeight="@dimen/abc_action_bar_default_height_material" >

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="@color/myTextPrimaryColor"
    android:layout_gravity="center" />

</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)