单击 EditText 视图时工具栏会扩展

Toolbar expanding on clicking EditText view

我已经在 MainActivity 中实现了 material 导航抽屉和工具栏。为了实现导航抽屉并确保它显示在状态栏后面,我使用了以下代码 toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primaryColor"
    android:fitsSystemWindows="true"
    android:theme="@style/ToolbarTheme">
</android.support.v7.widget.Toolbar>

并将其包含在 activity_main.xml 中,如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

在 styles.xml 我正在使用以下属性,

    <item name="android:windowTranslucentStatus">true</item>

一切正常,我得到以下结果,

当我单击编辑文本视图 (00.00) 时出现问题。单击页面中的编辑文本视图或任何编辑文本视图时,工具栏就会展开。见下图:

现在,我可以通过在 activity_main.xml 中放置 android:fitsSystemWindows="true" 来纠正问题,如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_drawer"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">
........

我的问题是,虽然这个解决方案有效,但我不确定为什么我必须在布局文件中两次使用相同的属性?什么使工具栏展开?另外,这是解决这个问题的正确方法吗?

编辑 - 从 toolbar.xml 中删除 android:fitsSystemWindows="true" 并将其放置在抽屉布局中导致主要 activity 产生正确的结果,但其他活动有一个白色条。见下图。

我和你有同样的问题,我会告诉你我做了什么来解决这个问题

1) 定义 toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolBarDetalleContacto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    app:layout_collapseMode="pin"
    app:theme="@style/Theme.GpsFriends.Material">

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

诀窍是在 activity xml 中的 <android.support.v4.widget.DrawerLayout> parent 组件中使用此属性,并在此处设置 属性 android:fitsSystemWindows="true"(也在你的 toolbar.xml 中)。如果你不使用 DrawerLayout,你的 colorPrimaryDark 属性中的永久灰色不会改变,将始终是灰色,只是灰色。

2) 定义你的 activity xml:

<android.support.v4.widget.DrawerLayout 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:orientation="vertical"

    android:fitsSystemWindows="true"
    tools:context="com.wherefriend.activities.agregaramigos.AgregarAmigosActivity">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            android:id="@+id/toolbar_main_gps_friends"
            layout="@layout/toolbar_gps_friends"
            ></include>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/bBuscar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:onClick="buscarAmigos"

            android:text="Buscar" />
        <EditText
            android:id="@+id/etfragmentCorreo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/bBuscar"
            android:ems="10"
            android:inputType="textEmailAddress" />

    </RelativeLayout>

    <ListView
        android:id="@+id/listaResultado"
        android:divider="@drawable/horizontal_linear_layout_divider"
        android:dividerHeight="0dp"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

如您所见,我首先将 child activity xml UI 定义为 DrawerLayout 但在下面我使用 <LinearLayout> 来包含我的整个用户界面 查看组件。首先是我的工具栏,然后是其他组件。

结果是这样的,在真实的 Nexus 4 中测试 - LG API versión 22: