简体   繁体   中英

Dialog changes the orientation by itself. How to fix the orientation of a dialog to landscape

I am showing a custom dialog in an activity of which the orientation is set to landscape. But the orientation is getting changed to portrait mode from the inital landscape(only the dialog, below activity remains in landscape mode). This is happening without any outside action like tilting the tablet or something.

I could not find any help in fixing the orientation of dialog anywhere.

Any ideas?

Dialog mDialog = new Dialog(MainActivity.this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mDialog.setContentView(R.layout.alert);
mDialog.show();

This is the code used for showing the dialog.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dip"
    android:layout_height="300dip"
    android:background="@drawable/alert_bg"
    android:orientation="vertical" >
    ------------------------------
    ---------------------------
</LinearLayout>

This is the xml element parent tag used.

If you are using an Activity as a dialog then you can set

android:screenOrientation="landscape" 

in your manifest file.

If you are using custom dialog then you can use

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

before you use setContentView(); method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM