我希望平板电脑能够显示肖像和景观(sw600dp或更大),但手机仅限于肖像。我找不到任何有条件地选择方向的方法。有什么建议吗?


当前回答

我在toMainActivity.java文件中添加了这个代码部分。它对我的案子起作用了。

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            handleOrientationConfiguration();
        }

        private void handleOrientationConfiguration() {

            if (isTablet()) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }

        private boolean isTablet() {
            Configuration configuration = this.getContext().getResources()
                    .getConfiguration();/*from   www .j ava  2  s. c  o  m*/
            return (configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
        }

其他回答

建议的方法是不锁定任何窗口维度的方向,但您可以通过遵循这里的指南来实现。

在步骤中,要做的第一件事是解锁舱单上的方向

<activity
    android:name=".MyActivity"
    android:screenOrientation="fullUser">

接下来,你可以使用Jetpack窗口管理器来确定应用程序在屏幕上有多少空间:

/** Determines whether the device has a compact screen. **/
fun compactScreen(): Boolean {
    val screenMetrics = WindowMetricsCalculator
                        .getOrCreate()
                        .computeMaximumWindowMetrics(this)
    val shortSide = min(screenMetrics.bounds.width(),
                        screenMetrics.bounds.height())
    return shortSide / resources.displayMetrics.density < 600
}

最后,当空间变小时,锁定方向,通常在手机上:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requestedOrientation = if (compactScreen())
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else
        ActivityInfo.SCREEN_ORIENTATION_FULL_USER
    ...
}

不幸的是,使用setRequestedOrientation(…)方法会导致活动重新启动,所以即使你在onCreate方法中调用这个,它也会经历活动生命周期,然后它会在请求的方向上重新创建相同的活动。所以在@Brian Christensen的回答中,你应该考虑到活动代码可能会被调用两次,这可能会产生不良影响(不仅是视觉上,而且在网络请求、分析等方面也会产生不良影响)。

此外,在清单中设置configChanges属性在我看来是一个很大的权衡,这可能会花费大量的重构成本。Android开发者不建议改变这个属性。

最后,尝试以不同的方式设置screenOrientation(以避免重新启动问题)是不可能的,静态不可能,因为静态manifest不能被改变,编程上只可能在已经启动的活动中调用该方法。

总结:在我看来,@Brian Christensen的建议是最好的权衡,但要注意重新启动活动的问题。

其他的解决方案对我都不起作用。在对话和娱乐方面,我仍然有一些奇怪的方向问题。我的解决方案是扩展活动,迫使它作为肖像在manifest。

例子:

public class MainActivityPhone extends MainActivity {}

manifest.xml:

        <activity
        android:screenOrientation="portrait"
        android:name=".MainActivityPhone"
        android:theme="@style/AppTheme.NoActionBar" />

在溅屏活动中:

    Intent i = null;
    boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
    if (!isTablet)
        i = new Intent(this, MainActivityPhone.class);
    else
        i = new Intent(this, MainActivity.class);
    startActivity(i);

我在toMainActivity.java文件中添加了这个代码部分。它对我的案子起作用了。

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            handleOrientationConfiguration();
        }

        private void handleOrientationConfiguration() {

            if (isTablet()) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }

        private boolean isTablet() {
            Configuration configuration = this.getContext().getResources()
                    .getConfiguration();/*from   www .j ava  2  s. c  o  m*/
            return (configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
        }

我在提供的解决方案中遇到了一些问题,最后这对我来说是有效的:

在AndroidManifest.xml中设置Activity的方向为“锁定”:

<activity
    android:name="com.whatever.YourActivity"
    android:screenOrientation="locked"
    ... />

在Activity中的OnCreate(…)方法中,使用以下方法:

@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
    if (isTablet(resources)) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    } else if (resources.configuration.orientation != Configuration.ORIENTATION_PORTRAIT) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }
    super.onCreate(savedInstanceState)
}

注意,你不需要在资源中使用布尔值的hack解决方案,你可以使用DeviceProperties类中的isTablet(资源)方法,除了其他东西之外,它检查是否smallstscreenwidthdp >= 600。