android activity landscape,Force an Android activity to always use landscape mode

该问题讨论了如何在Android中强制一个活动(Activity)始终处于横屏模式。解决方案涉及修改AndroidManifest.xml文件,设置屏幕方向为横屏,并可能需要在代码中调整以处理配置变化,例如通过调用`setRequestedOrientation()`方法。此外,还提到了在不同设备和Android版本上可能遇到的问题,如布局适应和内存泄漏,并提供了相应的解决建议。
摘要由CSDN通过智能技术生成

问题

I am using the Android VNC viewer on my HTC G1. But for some reason, that application is always in landscape mode despite my G1 is in portrait mode. Since the Android VNC viewer is open source, I would like know how is it possible hard code an activity to be 'landscape'. I would like to change it to respect the phone orientation.

1e8ad38a71c803b07f4f3c5805c27e7d.png

回答1:

Looking at the AndroidManifest.xml (link), on line 9:

This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden". This points to a overridden function in VncCanvasActivity.java.

If you look at VncCanvasActivity, on line 109 is the overrided function:

@Override

public void onConfigurationChanged(Configuration newConfig) {

// ignore orientation/keyboard change

super.onConfigurationChanged(newConfig);

}

The author specifically put a comment to ignore any keyboard or orientation changes.

If you want to change this, you can go back to the AndroidManifest.xml file shown above, and change the line to:

This should change the program to switch from portrait to landscape when the user rotates the device.

This may work, but might mess up how the GUI looks, depending on how the layout were created. You will have to account for that. Also, depending on how the activities are coded, you may notice that when screen orientation is changed, the values that were filled into any input boxes disappear. This also may have to be handled.

回答2:

You can set the same data in your java code as well.

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Other values on ActivityInfo will let you set it back to sensor driven or locked portrait. Personally, I like to set it to something in the Manifest as suggested in another answer to this question and then change it later using the above call in the Android SDK if there's a need.

回答3:

In my OnCreate(Bundle), I generally do the following:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

回答4:

You can specify the orientation of an activity in the manifest. See here.

...

android:screenOrientation=["unspecified" | "user" | "behind" |

"landscape" | "portrait" |

"sensor" | "nosensor"]

...

"adjustResize", "adjustPan"] >

回答5:

In the manifest:

android:screenOrientation="portrait"

android:configChanges="orientation|screenSize">

In your activity:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setContentView(R.layout.your_activity_layout);

回答6:

The following is the code which I used to display all activity in landscape mode:

android:configChanges="orientation|keyboardHidden"

android:name="abcActivty"/>

回答7:

A quick and simple solution is for the AndroidManifest.xml file, add the following for each activity that you wish to force to landscape mode:

android:screenOrientation="landscape"

回答8:

This works for Xamarin.Android. In OnCreate()

RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;

回答9:

That's it!! Long waiting for this fix.

I've an old Android issue about double-start an activity that required (programmatically) landscape mode: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Now Android make Landscape mode on start.

回答10:

Arslan,

why do you want to force orientation pro grammatically, though there's already a way in manifest

回答11:

Doing it in code is is IMO wrong and even more so if you put it into the onCreate. Do it in the manifest and the "system" knows the orientation from the startup of the app. And this type of meta or top level "guidance" SHOULD be in the manifest. If you want to prove it to yourself set a break in the Activity's onCreate. If you do it in code there it will be called twice : it starts up in Portrait mode then is switched to Landscape. This does not happen if you do it in the manifest.

回答12:

For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the landscape value.

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

Using only keyboardHidden|orientation would still result in memory leaks and recreation of my activities when pressing the power button.

回答13:

Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

回答14:

Press CTRL+F11 to rotate the screen.

来源:https://stackoverflow.com/questions/2150287/force-an-android-activity-to-always-use-landscape-mode

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值