问题
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.
回答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