2014-07-04 4 views
0

활동 화면 방향이 잠겨있는 경우 대화 상자 조각을 90도 또는 180도 회전하는 방법은 무엇입니까? 매니페스트에 선언대화 상자 조각을 강제로 회전시키는 방법은 무엇입니까?

활동 :

<activity 
    ... 
    android:screenOritentation="landscape" /> 

대화 조각 :

public class MyFragment extends DialogFragment{ 
    // stuff 
} 

나는 NineOldAndroid 라이브러리를 사용하여 대화 상자의 레이아웃을 회전했습니다. 180도 회전하면 예상대로 작동하지만 90도 회전하면 레이아웃이 완전히 보이지 않습니다.

전체 대화 상자 (레이아웃뿐만 아니라), 단추, 제목, 모든 것을 회전하려고하는데 어떻게해야하는지 알지 못했습니다.

답변

3

DialogFragment의 onStart() 및 onStop() 메서드에서 ActivityInfo의 플래그를 사용해 볼 수 있다는 주요 아이디어.

@Override 
public void onStart() 
{ 
    super.onStart(); 
    // lock screen; 
    DisplayTools.Orientation orientation = DisplayTools.getDisplatOrientation(getActivity()); 
     getActivity().setRequestedOrientation(orientation == DisplayTools.Orientation.LANDSCAPE 
       ? ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE 
       : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
} 

(ActivityInfo에서이 사용 뭔가 플래그)

그리고) (중지시에 방법 방향을 복원 :

을() 메서드 ONSTART에 대한 몇 가지 방향을 해결할 수 DialogFragment에 대한 예를 들어

@Override 
public void onStop() 
{ 
    super.onStop(); 
    // unlock screen; 
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
} 

(특별한 경우 ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE 대신 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED를 사용해야합니다. t 이동 중지() 메소드) 나는 당신이 주요 개념을 이해 바랍니다

public class DisplayTools 
{ 
    static public Point getDisplaySize(Context context) 
    { 
     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     return size; 
    } 

    static public Orientation getDisplatOrientation(Activity activity) 
    { 
     if (activity != null) 
     { 
      int orientation = activity.getResources().getConfiguration().orientation; 
      if (orientation == Configuration.ORIENTATION_LANDSCAPE) 
      { 
       return Orientation.LANDSCAPE; 
      } 
      if (orientation == Configuration.ORIENTATION_PORTRAIT) 
      { 
       return Orientation.PORTRAIT; 
      } 
      if (orientation == Configuration.ORIENTATION_UNDEFINED) 
      { 
       return Orientation.UNDEFINED; 
      } 
     } 
     return Orientation.UNKNOWN; 
    } 

    public static enum Orientation 
    { 
     UNKNOWN(-1), 
     UNDEFINED(0), 
     PORTRAIT(1), 
     LANDSCAPE(2); 

     private int tag; 

     Orientation(int i) 
     { 
      this.tag = i; 
     } 
    } 
} 

!

+1

이 문제는 활동이 재생성된다는 것입니다. –

1

해결책은 현재 방향으로 대화 상자의 특정 레이아웃을 부 풀리는 것입니다. 내 앱에서 액티비티는 가로로 고정되지만 기기를 세로 방향으로 돌리면 활동을 재생성하지 않고 특정 레이아웃을 보여줍니다.

참고 : 이미 열린 대화 상자에서 작동하지 않습니다. 장치 회전시 대화 상자가 회전되지 않습니다. 대화 상자에서 생성 할 때만 회전됩니다.

귀하의 활동

public class ActivityMain extends Activity 
{ 
    private boolean isPortrait = false; 
    private boolean isFlip = false; 

    private OrientationEventListener orientationEventListener; 

    @Override 
    protected void onCreate(Bundle bundle) 
    { 
     // stuff 

     setUpOrientationListener(); 
    } 

    // Register device to detected orientation change 
    private void setUpOrientationListener() 
    { 
      orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) 
      { 
       @Override 
       public void onOrientationChanged(int orientation) 
       { 
        // Device is in portrait 
        if (orientation > 320 || orientation < 45) 
        { 
         if (!isPortrait) 
          onPortraitRotation(); 
        } 
        else // Device is flipped 
        if (orientation > 45 && orientation < 145) 
        { 
         if (!isFlip) 
          onFlipRotation(); 
        } 
        else // Device is in landscape 
        { 
         if (isPortrait) 
          onLandscapeRotation(); 
        } 
       } 
      }; 

      // If device is capable for detecting orientation register listener 
      if (orientationEventListener.canDetectOrientation()) 
       orientationEventListener.enable(); 
     } 

    private void onPortraitRotation() 
    { 
     isPortrait = true; 
     isFlip = false; 
    } 

    private void onFlipRotation() 
    { 
     isFlip = true; 
     isPortrait = false; 
    } 

    private void onLandscapeRotation() 
    { 
     isPortrait = false; 
     isFlip = false; 
    } 

    // Creates your custom dialog 
    private void showCustomDialog() 
    { 
     MyDialog dialog = new MyDialog(ActivityMain.this, isPortrait, isFlipped); 
     dialog.show(); 
    } 
} 

지금 우리가 지금 대화 상자를 만들고, 우리의 장치의 방향을 알고있다.

참고 정적 생성자 사용 DialogFragment : http://developer.android.com/reference/android/app/DialogFragment.html (단, 내가 데모에 대한 간단한 대화 상자를 사용하고 있습니다)

public class MyDialog extends Dialog 
{ 
    private boolean isPortrait; 
    private boolean isFlipped; 

    public MyDialog(Context context, boolean isPortrait, boolean isFlipped) 
    { 
     super(context); 

     this.isPortrait = isPortrait; 
     this.isFlipped = isFlipped; 
    } 

    @Override 
    protected void onCreate(Bundle bundle) 
    { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

     // Device is in landscape mode 
     if (!isPortrait && !isFlipped) 
      setContentView(R.layout.dialog_landscape); 
     else 
     if (isPortrait && !isFlipped) // Device is in portrait 
      setContentView(R.layout.dialog_vertical); 
     else // Device is flipped 
     { 
      setContentView(R.layout.dialog_landscape); 

      // Rotate the entire root layout 
      View layout = findViewById(R.id.rlContainer); 

      ObjectAnimator.ofFloat(layout, "rotation", 180).setDuration(0).start(); 
     } 
    } 
} 

회전 NindeOldAndroids 라이브러리를 사용합니다.

참고 :이 솔루션은 가로, 세로 및 뒤집힌 풍경을 대상으로합니다.대칭 된 초상화를 표시하려면 방향 감지 수신기에서 방향을 계산해야합니다.

참고 2 : 시스템 버튼을 사용하지 마십시오! 부모 (활동) 잠긴 방향에 첨부 된 대화 상자에 첨부됩니다. 레이아웃 내장 단추보기를 사용하십시오.

0

나는 cosic's solution이 활동의 ​​구성과 함께, 최고라고 생각 :

android:configChanges="orientation|screenSize" 

가 ... 다음 활동에 당신이 onConfigurationChanged()을 구현하고 하나의 변화를 무시하거나 뭔가 사용자 정의 할 수 있습니다.

관련 문제