2012-04-02 8 views
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 

すなわち両方が実行されます 場合、他 Portraitモードに Landscape modeから切り替える場合。

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

私は絶対的な初心者ですモノラルのためのモノ、どのように助けていただきありがとうございます。

答えて

1

方法下記オーバライド)oncreate()

int prev_orientation =0; 

2の上方宣言)

1 ...このその作業をしてみてください。

@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)活動のmanifeastファイルの行の下に追加します。 :

android:configChanges="keyboardHidden|orientation" 
+0

だが、同じ出力:( – GAMA

+0

その作業かどうか?そのは –

0

iは、コードhere..tryを貼り付けています。この..

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()); 
    } 
+0

が試した私のために働いたが、uは任意のエラーを持っている起こる何:( – GAMA

+0

を動作していませんか? – Hasmukh

+0

エラーではありませんが、質問に記載されているものと同じ出力... – GAMA

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 
} 
+0

これは質問に対する回答であり、それが問題を解決する方法を説明することができますか? –

+0

オリエンテーション変更権利に対処したいですか?それはHasmukhの答えと同じですが、私のものはモノドロイドです。このコードでは、向きの変更に対処することができます。違いは、TSはConsole.WriteLineを実行したいと考えていますが、私は各方向に対して特定のレイアウトを開きます。 – Dobermaxx99

関連する問題