2012-01-29 56 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>