Android上的活动方向会自动更改

JJa*_*ck_ 11 java mobile android android-orientation android-activity

我正在开发基于Android的移动应用程序minSdkVersion=15.我想支持平板电脑的两种方向,只支持智能手机的肖像.一切都像魅力一样,但我遇到了一个让我发疯的小虫子.当智能手机处于横向模式并尝试触发新活动时,它会以横向模式打开一段时间,然后自动切换到纵向.我的每个活动都扩展了一个GeneralActivity类:

public class GeneralActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If smartphone lock orientation to portrait
        if (!Helper.isTablet(this.getApplicationContext())){
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我检测到具有此功能的平板电脑

public class Helper {
    public static boolean isTablet(Context context){
        Configuration config = context.getResources().getConfiguration()
        return config.smallestScreenWidthDp >= 600;
    }
}
Run Code Online (Sandbox Code Playgroud)

我选择不在android:screenOrientationManifest.xml中指定,因为这样我就可以支持平板电脑的所有界面方向.我错过了什么吗?

编辑

我决定应用Jonathan在答案中建议的最佳实践,但我所描述的问题仍然存在.这是我在github上的回购:https://github.com/giacmarangoni/Android-Orientation-Test

Hub*_*uby 13

我在Android N&O上遇到了同样的问题并找到了解决方案.

我仍然在onCreate方法中使用setRequestedOrientation,但是我已经为清单中的每个活动添加了这个:

android:screenOrientation="behind"
Run Code Online (Sandbox Code Playgroud)

这可确保活动以与上一个活动相同的方向启动.之后的setRequestedOrientation将覆盖它,但如果它与上一个活动相同,则您不会看到任何更改作为用户.


Cro*_*ono 5

我找到了使用以下方法解决此问题的正确方法:

android:screenOrientation="locked"
Run Code Online (Sandbox Code Playgroud)

在清单中与此问题一起声明的每个活动中。

并继续以setRequestedOrientation()编程方式使用onCreate()方法定义横向还是纵向,

顺便说一句,在您的GitHub项目中,我只是locked像这样更改清单添加方向,它终于起作用了:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.giacomomarangoni.orientationtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:screenOrientation="locked">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".OtherActivity"
            android:screenOrientation="locked">
        </activity>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

希望它也能对您有所帮助:)

  • 你是一个英雄。我通常不会只是为了表扬而发表评论,但我想向遇到此问题的其他人强调,这是正确的解决方案。它应该是公认的答案,特别是因为它允许任何方向锁定逻辑与标准 setRequestedOrientation 方法一起使用。 (4认同)

Jon*_*ste 0

这是使用资源和大小限定符的好方法。

将此 bool 资源作为 bools.xml 或其他内容放入 res/values 中(文件名在这里并不重要):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

将其放入 res/values-sw600dp 和 res/values-xlarge 中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

请参阅此补充答案以获取在 Android Studio 中添加这些目录和文件的帮助。

然后,在 Activity 的 onCreate 方法中,您可以执行以下操作:

if(getResources().getBoolean(R.bool.portrait_only)){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Run Code Online (Sandbox Code Playgroud)

最小宽度方向超过 600 dp 的设备,或者 Android 3.2 之前的设备(基本上是平板电脑)上的超大设备,基于传感器和用户锁定旋转等,其行为将与正常情况一样。其他一切(手机、漂亮的设备)很多)将仅是肖像。

来源:原答案