2012-05-22 36 views
1

所以我有一個活動Android的方向招

我需要它保持方向相同,一個設備是,當用戶打開活動(所以如果活動被打開時的景觀Web視圖 - >保持這種..如果肖像! - >記住這)

所以

  • 不需要XML清單符號..我不希望強制一個特定的方向,只是爲了讓開一個!

這兩個試作無可奈何:

public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(null); 

public void onConfigurationChanged(Configuration newConfig) { 
     newConfig.orientation = getResources().getConfiguration().orientation; 
     super.onConfigurationChanged(newConfig); 

所以想法?

回答

0

對於修復活動Oriention

集瑪尼宴活動

android:screenOrientation="portrait" 

或者

android:screenOrientation="landscape" 
+0

再次不想強制一個特定的方向..只要保持活動實例化的那個! – pulancheck1988

0

您應該機器人會活動週期讀了。 當你重新打開一個活動時,一個新的實例被構建。因此沒有「以前」的方向。 如果您希望您在暫停活動時具有相同的方向,則應將以前的方向存儲在貫穿整個生命週期的值中。

編輯:如果你想讓你的例子工作,你應該在清單中聲明它們。

0

將android:configChanges =「orientation」放入清單下的活動中,告訴android該活動將自己處理屏幕方向更改。

0

我不知道爲什麼你要實現該功能,但是這應該工作:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(savedInstanceState == null) // you only want to save orientation when you are loading the activity for the first time 
    { 
     ((MyApp)getApplication()).orientation = getResources().getConfiguration().orientation; // I saved the orientation in the application class, but you can save it anywhere you like except in "this" 
     Log.d("test", "orientation in onCreate: "+((MyApp)getApplication()).orientation); 
    } 

} 
@Override 
protected void onResume() { 
    super.onResume(); 
    Log.d("test", "setContentview with orientation in onResume "+((MyApp)getApplication()).orientation); 
    if(((MyApp)getApplication()).orientation==Configuration.ORIENTATION_LANDSCAPE) 
    { 
     setContentView(R.layout.my_layout_portrait); 
    } 
    else if(((MyApp)getApplication()).orientation ==Configuration.ORIENTATION_PORTRAIT) 
    { 
     setContentView(R.layout.my_layout_landscape); 
    } 
} 

在一個側面說明,這是一個壞主意,我以迫使像的方向。

+0

webview(js + html)將有奇怪的樣子,如果我不! – pulancheck1988

+0

好的,好吧...讓我知道,如果這對你有用。 –

+0

哦,我剛剛得到了另一個「偉大」的想法。爲什麼不在這個活動開始之前檢查方向? 如 if(orientation == Configuration.ORIENTATION_LANDSCAPE) –