Изменение макетов при изменении ориентации ошибки - PullRequest
0 голосов
/ 26 марта 2012

Я пытаюсь заставить мое приложение изменять макеты при изменении ориентации.Это код, который у меня есть:

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?

1 Ответ

4 голосов
/ 26 марта 2012

Вставить "}" после 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.Когда ориентация изменилась, вместо onCreate.

будет вызван onConfigurationChanged.
...