0

向きの変更に対処するために、アクティビティに次のコード スニペットを使用しました。

[Activity (Label = "Activity",ConfigurationChanges = ConfigChanges.Orientation 
| ConfigChanges.KeyboardHidden)]

public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);
            if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
            {
                Console.WriteLine("landscape");
            } 
            else if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
            {
                Console.WriteLine("portrait");
            }
        }

モードから始めて、Portraitモードに切り替えてLandscape mode、再びPortraitモードに戻ります。したがって、予想される出力は次のようになります。

landscape

portrait

しかし、 コンソール出力が表示されます

landscape

landscape

portrait

つまり、モードLandscape modeからPortraitモードに切り替えると、ifelseの両方が実行されます。

なぜこれが起こっているのか分かりません。

私はMono for Androidの初心者なので、助けていただければ幸いです。

4

3 に答える 3

1

これが私のコードです

if (Resources.Configuration.Orientation == Android.Content.Res.Orientation.Landscape)
{
    SetContentView(Resource.Layout.Main); //set layout for Landscape mode
}
else
{
    SetContentView(Resource.Layout.MainPortrait); //set layout for Portrait mode
}
于 2012-09-21T02:38:00.920 に答える
0

私はここにコードを貼り付けています..これを試してください..

public void onConfigurationChanged(Configuration orient) 
    {
        super.onConfigurationChanged(orient);

        // Checks the orientation of the screen
        if (orient.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
                      //code here for LANDSCAPE..
        ));   

        }
        else if (orient.orientation == Configuration.ORIENTATION_PORTRAIT)
        {           
                      //code here for PORTRAIT ..   
            }               
    }

メソッドを呼び出します。

  @Override
public void onCreate(Bundle savedInstanceState) 
{
        onConfigurationChanged(getResources().getConfiguration());
    }
于 2012-04-02T06:00:15.093 に答える
0

これを試してみてください...

1) 上記を宣言するoncreate()

int prev_orientation =0;

2) 以下のメソッドをオーバーライドします。

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        int orientation = getResources().getConfiguration().orientation;
        if(prev_orientation!=orientation){
            prev_orientation = orientation;
        if(orientation ==1){            
            //por
            Toast.makeText(getApplicationContext(),"ort::pot"+orientation,Toast.LENGTH_LONG).show();
        }else if(orientation ==2){
            //land
            Toast.makeText(getApplicationContext(),"ort::land"+orientation,Toast.LENGTH_LONG).show();
        }
        }
        super.onConfigurationChanged(newConfig);
    }

3) アクティビティのマニフェスト ファイルに以下の行を追加します。

android:configChanges="keyboardHidden|orientation"
于 2012-04-02T05:55:07.793 に答える