gpt4 book ai didi

java - Android 中相对布局中的动画重新定位其他 View

转载 作者:行者123 更新时间:2023-12-01 19:15:21 25 4
gpt4 key购买 nike

我的动画有问题,这是我的代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Handler().postDelayed(new Runnable(){

@Override
public void run() {
Animation anim= new TranslateAnimation(0, 0, 0, -200);
anim.setDuration(2000);
anim.setFillAfter(true);
findViewById(R.id.button1).startAnimation(anim);

}

}, 2000);
}

主要布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:text="Button"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="120sp"/>

<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:text="Button"
android:layout_height="fill_parent"
android:layout_below="@id/button1"
android:minHeight="120sp"/>

</RelativeLayout>

我希望按钮2与按钮1一起移动。我知道我可以对两个按钮应用相同的动画,并且它们一起移动,但随后按钮 2 将移至底部边缘。

有人知道我该如何修复它吗?

最佳答案

我不确定你真正想要实现什么。我想您希望 Button2 在动画播放后保留在布局的顶部。我说得对吗?

据我所知,在 Android 中,为其他 View 设置动画时 View 不会重新定位自己。当将 View 的可见性设置为“消失”时,它们会重新定位。

因此你可以:

  1. 对 Button2 应用不同的动画,以便将其移动到您想要的位置。
  2. 将 Button1 的可见性设置为动画结束后消失:

    button1.setVisibility( View.GONE );

关于java - Android 中相对布局中的动画重新定位其他 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6894044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com