2012-01-29 55 views
1

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

飞溅活动的代码如下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> 

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(); 

    } 

} 

代码,但问题是 - LISTSCREEN活动是重新创建的时间方向改变号码。

帮助我。

回答

6

我有一个类似的问题。我弄明白了。这是解决方案。 仅为闪屏创建一个布局,但为LANDSCAPEPORTRAIT创建不同的图像。由于您只有一个布局,即肖像,该活动不会被重新创建。但是背景图像会改变提供方向改变的效果。

在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); 
} 

覆盖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); 
    } 
} 

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> 
+0

也不要忘记加上'机器人:configChanges =“方向”'的Splash在AndroidManifest.xml – OleGG 2012-01-29 11:48:16

+0

活动说明@穆罕默德纳比尔 - 阿里夫感谢。 +1为您的建议,但我不想使用onConfigurationChanged方法和两个不同的图像。我希望单个图像在方向改变时旋转,我已经完成了。你可以在日食中检查它。我想要避免的唯一问题是在定位更改上重新创建LISTSCREEN活动。 – Vivek 2012-01-29 11:55:59

+0

在splashActivity的清单中添加android:screenOrientation =“portrait”。这将限制方向仅限于景观。所以活动不会被重新创建。 – 2012-01-29 12:13:52

2

这个工作对我来说...

清单中:改变方向,以android:screenOrientation="unspecified"

从飞溅布局中去除所有的排列元素例如android:orientation="vertical"

<activity 
     android:name="com.gr8ly.gr8lysymbolshortcutkeys.Splash" 
     android:label="@string/app_name" 
     android:screenOrientation="unspecified" 
     android:theme="@android:style/Theme.Black.NoTitleBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity>