如何在 Android 中为横向和纵向模式设置单独的启动画面?

How can I set separate splash screens for landscape and portrait mode in Android?

我正在开发一个 Android 应用程序,我需要在其中设置单独的初始屏幕,一个用于纵向,一个用于横向。两者是不同的图像。我该怎么做?

activity_splash.xml

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

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/wwe_logo" />

</RelativeLayout>

SplashScreen.java

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

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

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}

如果在制作layoutlayout-land后两个布局没有自动切换,那么您可以在onCreate中手动进行,然后再使用setContentView

 if (this.getWindow().getWindowManager().getDefaultDisplay()
                    .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
               setContentView(R.layout.activity_splash_portrait)
            } else {
               setContentView(R.layout.activity_splash_land)
            }

用相同的文件名制作2个不同的布局,并将横向布局放在layout-land中,纵向布局放在默认布局中 然后不需要处理来自 java class

的任何东西

按照以下步骤操作。

  1. 在与布局文件夹相同的res文件夹下创建文件夹layout-land。
  2. 接下来复制 activity_splash.xml 文件并粘贴到新文件夹 layout-land 中。 (此 activity_splash 是 SplashActivity java class 的 XML 文件)
  3. 然后将纵向启动图像更改为横向启动图像。 无需更改 SplashActivity java class 中的 java 代码以及清单文件。