具有方向支持的启动活动

Viv*_*vek 1 android orientation android-activity

我希望我的飞溅活动支持垂直和水平方向.

启动活动的代码如下

public class Splash extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Thread timer = new Thread(){
        public void run() {
            try{
                sleep(5000);    

            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent ARCActivityIntent = new Intent("com.ex.rc.LISTSCREEN");
                startActivity(ARCActivityIntent);// 
                finish();
            }
        }};

    timer.start();

    }

}
Run Code Online (Sandbox Code Playgroud)

splash.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    android:background="@drawable/splash_bg"
    >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但问题是 - LISTSCREEN活动重新创建方向更改的次数.

帮我.

Muh*_*rif 6

我遇到了类似的问题.我修好了.这是解决方案.仅为启动画面创建一个布局,但为LANDSCAPE和创建不同的图像PORTRAIT.由于您只有一个布局,即肖像,因此不会重新创建活动.但是背景图像会改变,从而提供方向变化的效果.

在onCreate中记下以下代码.

setContentView(R.layout.splash);

splashImage = (ImageView) findViewById(R.id.splash_img);

int orient = getWindowManager().getDefaultDisplay().getOrientation();
if (orient == 0) {
         splashImage.setBackgroundResource(R.drawable.splash_portrait);
} else {
         splashImage.setBackgroundResource(R.drawable.splash_landscape);
}
Run Code Online (Sandbox Code Playgroud)

覆盖onConfigurationChanged方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    int orient = getWindowManager().getDefaultDisplay().getOrientation();
    if (orient == 0) {
        splashImage.setBackgroundResource(R.drawable.splash_portrait);
    } else {
        splashImage.setBackgroundResource(R.drawable.splash_landscape);
    }
}
Run Code Online (Sandbox Code Playgroud)

清单中的SplashActivity应该如下所示

    <activity
        android:name="SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)