2012-03-26 76 views
0

我試圖讓我的應用程序在方向更改時更改佈局。這是我的代碼:當方向更改錯誤時更改佈局

package com.wish.list; 

import com.wish.list.R; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.OrientationEventListener; 
import android.view.WindowManager; 
import android.widget.ListView; 

public class ListOfLists extends Activity{ 

public void onCreate(Bundle savedInstanceState) { 


    // Orientation Change starts here: 
    private void setContentBasedOnLayout() 
     WindowManager winMan = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE); 

     if (winMan != null) 
     { 
      int orientation = winMan.getDefaultDisplay().getOrientation(); 

      if (orientation == 0) { 
       // Portrait 
       setContentView(R.layout.listsportrait); 
      } 
      else if (orientation == 1) { 
       // Landscape 
       setContentView(R.layout.listslandscape); 
      }    
     } 

    } 
    // End of Orientation Change 

     ListView listsList = (ListView) findViewById(R.id.lists); 
    } 
    } 
} 

上線

public void onCreate(Bundle savedInstanceState) { 

我得到這個錯誤:

「多個標誌這一行:

- 語法錯誤,插入「}」完成MethodBody

- 覆蓋android.app.Activity.onCreate」

此外,有沒有任何辦法去改變它,所以它不必重啓方向變化的活動?我擁有它,因此Facebook會在應用程序開始時檢查用戶的登錄詳細信息,並且每次方向更改時都必須重新檢查它。

編輯:

因此,代碼應該是現在這個樣子?

package com.wish.list; 

import com.wish.list.R; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.view.OrientationEventListener; 
import android.view.WindowManager; 
import android.widget.ListView; 

public class ListOfLists extends Activity{ 

public void onCreate(Bundle savedInstanceState) { 
} 
@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    super.onConfigurationChanged(newConfig); 

     // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     setContentView(R.layout.listslandscape); 
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
     setContentView(R.layout.listsportrait); 
    } 
} 
    // Orientation Change starts here: 
    private void setContentBasedOnLayout(){ 
     WindowManager winMan = (WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE); 

     if (winMan != null) 
     { 
      int orientation = winMan.getDefaultDisplay().getOrientation(); 

      if (orientation == 0) { 
       // Portrait 
       setContentView(R.layout.listsportrait); 
      } 
      else if (orientation == 1) { 
       // Landscape 
       setContentView(R.layout.listslandscape); 
      }    
     } 


    // End of Orientation Change 

     ListView listsList = (ListView) findViewById(R.id.lists); 
}  
} 

或者我刪除setContentBasedOnLayout部分並將其替換爲onConfigurationChanged代碼?

回答

4

插入「}」的onCreate

如果你不希望重啓活動時的方向改變,

下面1.Declare在清單:

<activity android:name=".MyActivity" 
      android:configChanges="orientation|keyboardHidden" 
      android:label="@string/app_name"> 

2.覆蓋活動中的onConfigurationChanged

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

     // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     setContentView(R.layout.listslandscape); 
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
     setContentView(R.layout.listsportrait); 
    } 
} 

編輯

是的,你應該刪除setContentBasedOnLayout部分。當方向改變時,將調用onConfigurationChanged而不是onCreate

+0

帶問題/新代碼的編輯OP – Cole 2012-03-26 03:46:10

+0

@Cole你看到我的回答? – dreamtale 2012-03-26 12:30:27