Animation in a relative layour repositioning the other views Android
I have a problem with the animations, here is my code:
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);
}
And the main layout is:
<?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:minHe开发者_如何学Cight="120sp"/>
</RelativeLayout>
I want the button2 move together with the button1. I know I can apply the same animation to both buttons and they move together but then the button2 will move away to the bottom margin.
Does somebody know how can I fix it?
I'm not sure of what you really want to achieve. I guess you would like button2 to remain at the top of your layout after the animation. Am I right?
As far as I know, in Android, views don't reposition themselves when animating other views. They do reposition when setting a view's visibility to GONE.
Therefore you could:
- Apply a different animation to button2 in order to move it where you want it to be.
Set visibility of button1 to gone after the animation:
button1.setVisibility( View.GONE );
Firstly, Android animations don't affect layout. Your button hasn't really moved, it just looks like it has.
Secondly, if you want both buttons to move together, put them in a linear layout and animate that instead.
精彩评论