使用XML中的"animateLayoutChanges"属性设置动画布局

u3l*_*u3l 9 xml android android-animation android-layout

我有一个LinearLayout,我android:animateLayoutChanges="true"在父母那里申请LinearLayout.当用户单击切换按钮时,LinearLayout"折叠"(我以LinearLayout.GONE编程方式设置视图的可见性,当他们再次单击它时,它通过以编程方式将可见性设置回来进行扩展LinearLayout.VISIBLE.

折叠和扩展的动画正常工作.

但是,在折叠LinearLayout动画完成之前,可折叠/可展开按钮下方的任何项目都会回到顶部.正在回收的项目不在animateLayoutChanges设置为的父项内true,我认为没有任何方法可以将它们放入其中.

这是我的布局层次结构(我没有提到保持简短的属性):

<!-- Top most LinearLayout-->
<LinearLayout>

    <!-- LinearLayout containing android:animateLayoutChanges="true"-->
    <LinearLayout>

        <!-- RelativeLayout containing button to toggle LinearLayout visibility below-->
        <RelativeLayout>
        </RelativeLayout>

        <!-- LinearLayout that has its visibility toggled -->
        <LinearLayout>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

整个布局以编程方式插入另一个LinearLayout(见下文):

<LinearLayout
    android:id="@+id/form_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- This is where the previous XML layout containing the toggle-able LinearLayout
         is inserted programmatically. -->

    <!-- This button snaps back up to the top before the animation is complete. -->
    <Button />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我意识到问题将会解决,如果我添加了Button快照,LinearLayoutanimateLayoutChanges就是真的.但是,出于几个原因,这不是一种选择.

那还有其他的工作吗?

Fay*_*yez 0

只是一个想法...如果从嵌入布局中删除“animateLayoutChanges”并将其添加到父布局(第二个 XML)中会怎样?我怀疑这会让一切变得生动起来。由于您是以编程方式嵌入布局,因此您可能必须在代码中将该属性设置为 true。

以下是如何以编程方式执行此操作 Stack-overflow

另一种选择是以编程方式在弹回按钮上使用 .animate 属性

myButton.animate().translationY(floatYposition).setDuration(300); //300 milliseconds
Run Code Online (Sandbox Code Playgroud)