Wie kann ich den Querformatmodus für einige Ansichten in meiner Android-App deaktivieren?
Antworten
Zu viele Anzeigen?Wenn Sie sich nicht die Mühe machen wollen, die Orientierung in jedem Manifest-Eintrag der Aktivität hinzuzufügen, erstellen Sie besser eine BaseActivity-Klasse (erbt 'Activity' oder 'AppCompatActivity'), die von jeder Aktivität Ihrer Anwendung anstelle von 'Activity' oder 'AppCompatActivity' geerbt wird, und fügen Sie einfach das folgende Stück Code in Ihre BaseActivity ein:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// rest of your code......
}
Tragen Sie es in Ihr Manifest ein.
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortrait" />
Die Ausrichtung ist Hochformat, aber wenn das Telefon des Benutzers auf dem Kopf steht, wird es auch in der richtigen Richtung angezeigt. (Ihr Bildschirm wird also um 180 Grad gedreht.)
Das System ignoriert dieses Attribut, wenn die Aktivität in Mehrfenstermodus .
Mehr: https://developer.Android.com/guide/topics/manifest/activity-element
So ändern Sie die Ausrichtung in einigen der Ansichten
Anstatt die Ausrichtung der gesamten Aktivität zu sperren, können Sie diese Klasse verwenden, um die Ausrichtung einer beliebigen Ansicht dynamisch und pragmatisch zu sperren:
Machen Sie Ihre Ansicht Landschaft
OrientationUtils.lockOrientationLandscape(mActivity);
Machen Sie Ihre Ansicht Porträt
OrientationUtils.lockOrientationPortrait(mActivity);
freischalten Orientierung
OrientationUtils.unlockOrientation(mActivity);
Orientierung Util Klasse
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Surface;
import android.view.WindowManager;
/* * This class is used to lock orientation of android app in nay android devices
*/
public class OrientationUtils {
private OrientationUtils() {
}
/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_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 (!(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)) {
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);
}
}