Antworten:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Wird eine Aktivität aufgerufen, wird sie für die Landschaft gesperrt. Suchen Sie nach den anderen Flags in der ActivityInfo-Klasse. Sie können es wieder im Hochformat sperren oder es sensor- / schiebereglergesteuert machen.
Weitere Informationen hier: http://www.devx.com/wireless/Article/40792
Achten Sie auf den Unterschied zwischen dem, was getConfiguration zurückgibt, und dem, was setRequestedOrientation wünscht - beide sind int, stammen jedoch aus unterschiedlichen konstanten Definitionen.
Hier erfahren Sie, wie Sie die aktuelle Ausrichtung sperren und dabei 180-Grad-Flips zulassen
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
Dies funktioniert auf Geräten mit umgekehrtem Hoch- und Querformat.
Schloss Orientierung:
int orientation = getActivity().getRequestedOrientation(); int rotation = ((WindowManager) getActivity().getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } getActivity().setRequestedOrientation(orientation);
Orientierung freischalten:
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
"Returns the rotation of the screen from its "natural" orientation."
abrufen . Auf einem Telefon, auf dem ROTATION_0 als Hochformat angezeigt wird, ist dies wahrscheinlich korrekt. Auf einem Tablet ist die "natürliche" Ausrichtung jedoch wahrscheinlich Querformat, und ROTATION_0 sollte Querformat anstelle von Hochformat zurückgeben.
Ich schien einen ähnlichen Fall gehabt zu haben. Ich wollte jede Ausrichtung unterstützen, musste aber nach einem bestimmten Punkt im Workflow in der aktuellen Ausrichtung bleiben. Meine Lösung war:
Beim Eintritt in den geschützten Workflow:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Beim Beenden des geschützten Workflows:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Alternative zur @ pstoppani-Antwort mit Unterstützung für Tablets (Wie bei der @ pstoppani-Antwort funktioniert dies nur auf Geräten> 2.2)
-Test auf Samsung Galaxy SIII
undSamsung Galaxy Tab 10.1
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int tempOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
switch(tempOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
activity.setRequestedOrientation(orientation);
}
||
in rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90
und mit rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270
. Also ich habe 2 Zweifel :::: erstens, warum ROTATION_0
statt ROTATION_180
im zweiten Fall und ein anderes warum 0 Grad mit 90 nicht 180 prüfen ??
||
Überprüfungen behandeln also die beiden möglichen Standardausrichtungen basierend auf der Berichtslandschaft des Geräts im Vergleich zum Hochformat.
Hier ist mein Code, Sie könnten mit einer dieser Methoden Ihren Bildschirm sperren und nach Abschluss der Aufgabe ihn mit entsperren entsperren:
/** Static methods related to device orientation. */
public class OrientationUtils {
private OrientationUtils() {}
/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/** Locks the device window in portrait mode. */
public static void lockOrientationPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/** Locks the device window in actual screen mode. */
public static void lockOrientation(Activity activity) {
final int orientation = activity.getResources().getConfiguration().orientation;
final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
// Copied from Android docs, since we don't have these values in Froyo 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
// Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
if (!BuildVersionUtils.hasGingerbread()) {
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90){
if (orientation == Configuration.ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
{
if (orientation == Configuration.ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}
/** Unlocks the device window in user defined screen mode. */
public static void unlockOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}
Hier ist die Xamarin-Konvertierung der obigen Antwort von @pstoppani.
HINWEIS: Dies ist für ein Fragment, ersetzen Sie Aktivität. mit diesem. wenn innerhalb einer Aktivität verwendet.
public void LockRotation()
{
ScreenOrientation orientation;
var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation;
switch (surfaceOrientation) {
case SurfaceOrientation.Rotation0:
orientation = ScreenOrientation.Portrait;
break;
case SurfaceOrientation.Rotation90:
orientation = ScreenOrientation.Landscape;
break;
case SurfaceOrientation.Rotation180:
orientation = ScreenOrientation.ReversePortrait;
break;
default:
orientation = ScreenOrientation.ReverseLandscape;
break;
}
Activity.RequestedOrientation = orientation;
}
public void UnlockRotation()
{
Activity.RequestedOrientation = ScreenOrientation.Unspecified;
}
Dies ist nicht getestet, da es vor der Verwendung mit einem anderen Ansatz durchgeführt wurde, kann jedoch von Nutzen sein.