方向更改时更改布局出错



当方向发生变化时,我正试图让我的应用程序更改布局。这是我的代码:

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代码替换它?

onCreate之后插入"}"

如果你不想在方向改变时重新开始活动,

1.在清单中声明如下:

<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

最新更新