해결책은 현재 방향으로 대화 상자의 특정 레이아웃을 부 풀리는 것입니다. 내 앱에서 액티비티는 가로로 고정되지만 기기를 세로 방향으로 돌리면 활동을 재생성하지 않고 특정 레이아웃을 보여줍니다.
참고 : 이미 열린 대화 상자에서 작동하지 않습니다. 장치 회전시 대화 상자가 회전되지 않습니다. 대화 상자에서 생성 할 때만 회전됩니다.
귀하의 활동
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 : 시스템 버튼을 사용하지 마십시오! 부모 (활동) 잠긴 방향에 첨부 된 대화 상자에 첨부됩니다. 레이아웃 내장 단추보기를 사용하십시오.
이 문제는 활동이 재생성된다는 것입니다. –