cannot change layout with animation in android

2019-09-07 22:58发布

问题:

I want to change layouts using TranslateAnimation.

My XML is like

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



<Button
    android:id="@+id/buttonsld"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textsld"
    android:text="slideup" />

<LinearLayout
    android:id="@+id/layoutsld2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/buttonsld2"
    android:layout_below="@+id/buttonsld"
    android:background="@drawable/iz"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:id="@+id/layoutsld3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/buttonsld2"
    android:layout_below="@+id/buttonsld"
    android:background="@drawable/fon"
    android:orientation="vertical"
    android:visibility="gone" >
</LinearLayout>

<Button
    android:id="@+id/buttonsld2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="slidedown" />

</RelativeLayout>

and my code is like

 private int x= 0;

buttonslideup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (x == 0) {
                    lin_lay2.setVisibility(View.GONE);
                    lin_lay3.setVisibility(View.VISIBLE);
                     Animation slide = new TranslateAnimation(0, 0, 100, 0);
                     slide.setDuration(1000);
                     slide.setFillAfter(true);

                     lin_lay3.startAnimation(slide);
                    x = 1;
                } else {
                    lin_lay3.setVisibility(View.GONE);

                    lin_lay2.setVisibility(View.VISIBLE);
                     Animation slide = new TranslateAnimation(0, 0,
                     100, 0);

                     slide.setDuration(1000);
                     slide.setFillAfter(true);
                     lin_lay2.startAnimation(slide);
                    x = 0;
                }

            }
        });

but when I use this, at first, the lin_lay3 slides and comes to screen and there is no problem, but when I click to button again, lin_lay2 slides, but it is behind lin_lay3 and I cannot see lin_lay2.

What I want is to change these layouts with the click of the button.

回答1:

Inside the on create, we need to type:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


回答2:

You start another activity on button click & see the animation of your activity layout. Just put this code after starActivity(intent).

overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);

slide_in_top.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@integer/slideanim"
    android:fromYDelta="-100%p"
    android:toYDelta="0" />

slide_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@integer/slideanim"
    android:fromYDelta="0"
    android:toYDelta="100%p" />

In value folder, create integer.xml and put

<item name="slideanim" format="integer" type="integer">1000</item>