Screen/view 在活动之间切换

Screen/view shifting between activities

完全是新手...当我从第一个 activity 调用第二个 activity 时,屏幕视图有点像 jumps/shifts。当我按下返回按钮 return 到第一个 activity 时,也会发生同样的情况。这些文件是由 Android Studio 生成的,我只添加了一个带有 onClick 的按钮来调用第二个 activity,并修改了操作栏文本。希望屏幕录像说明。知道是什么原因造成的吗?

录屏转gif动画

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/enter_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="120dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:text="Button"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".Main2Activity">

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.philburfitt.test;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // change text on actionbar
        getSupportActionBar().setTitle("Test - First Activity");

        // call Main2Activity on button press
        Button enterButton = findViewById(R.id.enter_button);
        enterButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
            }
        } );
    }
}

Main2Activity.java

package com.philburfitt.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        getSupportActionBar().setTitle("Test - Second Activity");
    }
}

您看到的是默认过渡,但是您可以使用自定义过渡。我将分享一个示例。

这就是我从 activity A 调用下一个 activity B 的方式。

Intent i = new Intent(getApplicationContext(), Dashboard.class);
       startActivity(i);
       finish();
       overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

现在在res>anim中添加这个动画文件

slide_in_right.xml

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

slide_out_left.xml

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