【问题标题】:Toolbar expanding on clicking EditText view单击 EditText 视图时扩展工具栏
【发布时间】:2015-08-05 05:59:47
【问题描述】:

我在 MainActivity 中实现了材质导航抽屉和工具栏。为了实现抽屉式导航并确保它显示在状态栏后面,我使用了工具栏.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) 时出现问题。单击页面中的编辑文本视图或任何编辑文本视图的那一刻,工具栏就会展开。见下图:

现在,我可以通过将android:fitsSystemWindows="true" 放入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:fitsSystemWindows="true"
    android:layout_height="match_parent">
........

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

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

【问题讨论】:

  • 您使用了两次哪个属性?而且我相信这是android:fitsSystemWindows="true" 的正确用法,因为它旨在使您的视图适合操作栏或导航栏的边缘
  • 一次在toolbar.xml中使用android:fitsSystemWindows="true"(已经包含在activity_main.xml中),另一次在android.support.v4.widget.DrawerLayout中再次出现在activity_main.xml中。
  • 您需要 android:fitsSystemWindows="true"DrawerLayout 以使其适合工具栏的边缘,以防止它进入工具栏下方,但我不确定为什么在工具栏中需要它.xml。如果删除会怎样?
  • 添加了删除android:fitsSystemWindows="true"的结果。请查看编辑。
  • stackoverflow.com/questions/28043202/… 这是其中一种解决方案。我试过了,它奏效了。不知道为什么,但这没关系:P

标签: android android-layout toolbar android-toolbar


【解决方案1】:

我遇到了和你一样的问题,我会告诉你我做了什么来解决它

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>

诀窍是在 &lt;android.support.v4.widget.DrawerLayout&gt; 父组件内的活动 xml 中使用此属性,并在此处设置属性 android:fitsSystemWindows="true"(也在您的工具栏.xml 中)。如果您不使用 DrawerLayout,您的 colorPrimaryDark 属性中的永久灰色颜色将不会更改,将始终为灰色且只是灰色。

2) 定义您的活动 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>

如您所见,我将第一个子活动 xml UI 定义为 DrawerLayout,但在下面我使用 &lt;LinearLayout&gt; 包含我的整个用户界面视图组件。首先是我的工具栏,然后是其余的组件。

结果就是这样,在真正的 Nexus 4 中测试 - LG API 版本 22:

【讨论】:

  • 我爱你,这解决了我正在开发的应用程序中的许多问题
猜你喜欢
  • 2018-05-30
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多