android - 使用 animateLayoutChanges ="true"时,内容被推到工具栏下方

标签 android android-layout android-animation

我使用 Navigation Drawer Activity 创建了一个新的 Android Studio 项目。

然后我在 content_main.xml 的 LinearLayout 中添加了一个简单的 TextView。 对于这个 LinearLayout(也用标准的 RelativeLayout 尝试过)我添加了 android:animateLayoutChanges="true" 参数。

如果我现在在 GONE 和 VISIBLE 之间切换 TextView 的可见性,content_main.xml 的整个根 LinearLayout 将被推上去并隐藏在工具栏下。

这是我的 content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity"
android:background="#84d8ff">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="207dp" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0004"
    android:animateLayoutChanges="true">

    <TextView android:text="Lorem Ipsum..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/test_item"
        android:layout_marginTop="29dp" />
</LinearLayout>

这是一张图片:

picture

蓝色背景属于content_main.xml的根LinearLayout,绿色背景属于包含content_main.xml的app_bar_main.xml

有谁知道为什么会这样以及如何解决?

最佳答案

我遇到了同样的问题。这可以通过打开布局 app_bar_main.xml 来解决

你找到了这条线:

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

改成这样:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="bottom">

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

</LinearLayout>

这是一个丑陋的修复,但它现在有效。

如果出现其他问题,您可以通过将其添加为根布局的直接子级来修复它:

<android.support.v4.widget.Space
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

</android.support.v4.widget.Space>

关于android - 使用 animateLayoutChanges ="true"时,内容被推到工具栏下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344511/

相关文章:

android - 使用按钮旋转相对布局 - 触摸事件区域不旋转

java - 如何制作android下载事件?

android - 多个 dex 文件定义 XmlPullParserException

java - 将单个字符转换为 CharSequence 的最有效方法

android - AnimationDrawable 从特定帧开始?

java - Espresso 单元测试 : How to disable animation in code

android - 找不到androidx.navigation :safe-args-gradle-plugin:1. 0.0-alpha01

android - 如何在 Jetpack Compose 中移动 slider 时消除拖动效果

android - 如何使用 layout_gravity 属性将 View 置于其父 View 的中心

android - 横向布局不会随着方向变化而变化,纵向布局不会随着方向变化而变化