我一直在尝试在我的Android应用程序中使用Coordinator Layout。我有一个应用栏布局和一个嵌套滚动视图在协调布局中。在我的嵌套滚动视图中,我有一个线性布局,其中animateLayoutChanges设置为true。
我的问题是,在线性布局高度增加时,并将项目的可见性设置为Visible时,线性布局会进入Appbar布局下方。只有在点击屏幕或滚动后,才会出现正确的滚动效果。
我创建了一个简单的应用程序来显示这个问题。下面是布局。
在点击显示按钮时,我将TextView设置为可见状态。请看图片以理解我的问题。
图片1- 初始状态。
图片2- 这就是问题所在。我已经点击了Show按钮。现在由于布局变化动画,线性布局移动到了AppBar Layout下面。如您所见,Show按钮已经移动到了App Bar下面。
图片3- 现在当我触摸屏幕或滚动时,滚动变得正常。
请帮忙。我一直在努力解决这个问题。谢谢。
我的问题是,在线性布局高度增加时,并将项目的可见性设置为Visible时,线性布局会进入Appbar布局下方。只有在点击屏幕或滚动后,才会出现正确的滚动效果。
我创建了一个简单的应用程序来显示这个问题。下面是布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context="testapp.test.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:animateLayoutChanges="true"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:id="@+id/test_Button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide"
android:id="@+id/test_Button2"/>
<TextView
android:id="@+id/test_tv"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone"
android:background="@color/colorAccent"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
在点击显示按钮时,我将TextView设置为可见状态。请看图片以理解我的问题。
图片1- 初始状态。
图片2- 这就是问题所在。我已经点击了Show按钮。现在由于布局变化动画,线性布局移动到了AppBar Layout下面。如您所见,Show按钮已经移动到了App Bar下面。
图片3- 现在当我触摸屏幕或滚动时,滚动变得正常。
请帮忙。我一直在努力解决这个问题。谢谢。
![Initial State.](https://istack.dev59.com/1vfVL.webp)
![Here is the problem. I have clicked Show. Now the Linear layout has moved under App Bar Layout due to animation.](https://istack.dev59.com/mPUwE.webp)
![Now when I touch screen or scroll, the scrolling becomes proper.](https://istack.dev59.com/FdMvD.webp)