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

1 回答 1

4

在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

于 2012-03-26T02:34:09.810 回答