2012-03-26 64 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