2012-04-02 3 views
0

방향 변경을 처리하기 위해 다음 활동에 대한 코드 스 니펫을 사용했습니다. android의 방향 변경 작업

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"); 
      } 
     } 

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

나는 Portrait 모드, 다음 Landscape mode로 전환하고 다시 Portrait 모드로 다시 전환 시작합니다. 그래서 예상 출력 같아야

landscape 

portrait 

하지만 콘솔 출력 보여준다

landscape 

landscape 

portrait 

즉 모두 실행 도착 경우 다른 Portrait 모드로 Landscape mode에서 전환하는 경우.

왜 이런 일이 발생하는지 잘 모르겠습니다.

나는 절대적인 초보자이다. 모노 용 안드로이드, 그래서 어떤 도움을 주셨습니다.

@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 파일에서 선 아래 추가

답변

1

이 그 작업을 시도 ...

1) oncreate()

int prev_orientation =0; 

2) 방법 아래 오버라이드 (override)하는 위의 선언 :

android:configChanges="keyboardHidden|orientation" 
+0

이 시도 코드,하지만 동일한 출력 :( – GAMA

+0

의 작업의 여부? 그것은 –

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 .. 
      }    
    } 

호출 방법 Like;

@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의 대답과 동일하지만 내 것은 monodroid에있다. h이 코드를 사용하면 방향 변경을 처리 할 수 ​​있습니다. 차이점은 TS가 Console.WriteLine을 원하지만, 내 방식은 각 방향에 대해 특정 레이아웃을 열 것입니다. – Dobermaxx99