我希望平板电脑能够显示肖像和景观(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);

以下是我的做法(灵感来自http://androidblogger.blogspot.com/2011/08/orientation-for-both-phones-and-tablets.html):

在AndroidManifest.xml中,对于每个你想要在纵向和横向之间改变的活动(确保你添加了screenSize -你不需要这个!)你不需要在这里设置屏幕方向。:

android:configChanges="keyboardHidden|orientation|screenSize"

在每个活动中添加的方法:

public static boolean isXLargeScreen(Context context) {
    return (context.getResources().getConfiguration().screenLayout
    & Configuration.SCREENLAYOUT_SIZE_MASK)
    >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
} 

and:(如果你不重写这个方法,应用程序将在改变方向时调用onCreate())

@Override
public void onConfigurationChanged (Configuration newConfig)
{       
    super.onConfigurationChanged(newConfig);

    if (!isXLargeScreen(getApplicationContext()) ) {            
        return; //keep in portrait mode if a phone      
    }

    //I set background images for landscape and portrait here
}

在每个Activity的onCreate()中:

if (!isXLargeScreen(getApplicationContext())) { //set phones to portrait; 
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);            
}
else {
  //I set background images here depending on portrait or landscape orientation 
}

我唯一不明白的是如何让应用程序改变布局文件时,从横向到纵向或反之亦然。我假设答案是做类似于上面的链接,但我不能让它为我工作-它删除了我所有的数据。但如果你有一个足够简单的应用,你有相同的纵向和横向布局文件,这应该是可行的。

I recently came across this requirement but didn't want to use any of these solutions. All of them have in common that they call setRequestedOrientation programmatically. The problem with this is it sets the orientation too late and causes slight UI glitches. If your device is in landscape when you launch the app it loads in landscape then rotates. This is particularly noticeable if you use a theme to create a splash screen effect as you will see the background image in the wrong orientation. This can also have a side effect on how your app is shown in the recent apps and issues with configuration change as noted in other answers comments.

需要在清单中设置正确的方向,这样系统就可以在不启动应用程序的情况下知道方向。

这里有一个解决方案(这是一个相当大的努力,但你会睡得更好)

首先将清单中的方向设置为占位符

<activity
      android:screenOrientation="${screenOrientation}"
     ...
</activity>

然后我们需要添加一个normal/tablet flavor来设置app/build.gradle中的值。(如果你已经在使用味道,可以用新的构建类型来实现)

android {
    ...
    productFlavors {
        normal {
            manifestPlaceholders = [screenOrientation: "portrait"]
        }
        tablet {
            manifestPlaceholders = [screenOrientation: "unspecified"]
        }
    }
}

现在我们需要告诉平板电脑版本,它只适用于大型设备。这可以通过添加只使用平板电脑的清单来与默认清单合并来实现。在->添加一个新的清单文件

app
 src
  tablet
   AndroidManifest.xml

下面是这个清单所需的全部内容,因为它与默认的清单合并为->

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

    <supports-screens
        android:smallScreens="false"
        android:normalScreens="false"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:requiresSmallestWidthDp="600"
        />

</manifest>

最后一个技巧是我们需要通过版本代码来区分构建,因为Playstore不能有两次带有匹配代码的上传。我们希望确保平板电脑(更严格的构建)具有更高的代码。一个简单的方法是取一个基本代码,然后平板电脑的基本* 2和普通的基本* 2 - 1。我使用CI与基本代码构建数字,但很容易在你的口味硬编码。

现在制作两种口味

 app-normal.apk/aab (v1.0.0, version code 1)
 app-tablet.apk/aab (v1.0.0, version code 2)

将它们以多个apk/aab的形式上传到Playstore,然后如果在平板电脑上下载,Playstore会提供旋转的apk,如果在手机上下载,则只提供一个纵向的apk。

注:仅适用于通过谷歌Playstore/Amazon Kindle分发

进一步阅读https://developer.android.com/google/play/publishing/multiple-apks https://developer.amazon.com/docs/fire-tablets/ft-screen-layout-and-resolution.html