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 に答える