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


当前回答

对已接受答案的补充

您可以在Android Studio中执行以下步骤来添加res/values-sw600dp和res/values-large目录及其bools.xml文件。

values-sw600dp

首先,在Project选项卡中选择导航器中的Project(而不是Android)过滤器。

然后右键单击app/src/main/res目录。选择“新建> Android资源目录”。

选择最小屏幕宽度,然后按>>按钮。

输入600作为最小的屏幕宽度。目录名称将自动生成。说好的。

然后右键单击新创建的values-sw600dp文件。选择“新建>值资源文件”。为名称键入bool。

values-large

只有在支持Android 3.2 (API级别13)之前,才需要添加一个值很大的目录。否则,您可以跳过此步骤。values-large目录对应于values-sw600dp。(values-xlarge对应values-sw720dp。)

要创建values-large目录,请执行与上面相同的步骤,但在本例中选择“大小”而不是“最小屏幕宽度”。选择大。目录名称将自动生成。

像以前一样右键单击该目录以创建bools.xml文件。

其他回答

以下是我的做法(灵感来自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 
}

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

根据Ginny的回答,我认为最可靠的方法如下:

如本文所述,在资源sw600dp中放入一个布尔值。它必须有前缀sw,否则它将无法正常工作:

在res / values-sw600dp / dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">true</bool>
</resources>

在res /价值/ dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">false</bool>
</resources>

然后创建一个方法来检索布尔值:

public class ViewUtils {
    public static boolean isTablet(Context context){
        return context.getResources().getBoolean(R.bool.isTablet);
    }
}

和一个base activity来扩展你想要的行为:

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!ViewUtils.isTablet(this)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}

因此每个活动都将扩展BaseActivity:

public class LoginActivity extends BaseActivity //....

重要:即使你从BaseActivity扩展,你必须添加行android:configChanges="orientation|screenSize"到每个活动在你的AndroidManifest.xml:

    <activity
        android:name=".login.LoginActivity"
        android:configChanges="orientation|screenSize">
    </activity>

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

在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。

我知道这是个老问题。为了让你的应用程序总是在纵向模式下运行,即使方向可能会被交换(例如在平板电脑上),我设计了这个功能,用于将设备设置在正确的方向上,而不需要知道设备上的纵向和横向功能是如何组织的。

   private void initActivityScreenOrientPortrait()
    {
        // Avoid screen rotations (use the manifests android:screenOrientation setting)
        // Set this to nosensor or potrait

        // Set window fullscreen
        this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        DisplayMetrics metrics = new DisplayMetrics();
        this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

         // Test if it is VISUAL in portrait mode by simply checking it's size
        boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); 

        if( !bIsVisualPortrait )
        { 
            // Swap the orientation to match the VISUAL portrait mode
            if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
             { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
            else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
        }
        else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }

    }

效果好极了!

注意: 改变这种情况。活动由您的活动或添加到主活动,并删除此。活动;-)

如果你想做相反的事情,你必须将代码更改为横向(但我认为这是很清楚的)。

对已接受答案的补充

您可以在Android Studio中执行以下步骤来添加res/values-sw600dp和res/values-large目录及其bools.xml文件。

values-sw600dp

首先,在Project选项卡中选择导航器中的Project(而不是Android)过滤器。

然后右键单击app/src/main/res目录。选择“新建> Android资源目录”。

选择最小屏幕宽度,然后按>>按钮。

输入600作为最小的屏幕宽度。目录名称将自动生成。说好的。

然后右键单击新创建的values-sw600dp文件。选择“新建>值资源文件”。为名称键入bool。

values-large

只有在支持Android 3.2 (API级别13)之前,才需要添加一个值很大的目录。否则,您可以跳过此步骤。values-large目录对应于values-sw600dp。(values-xlarge对应values-sw720dp。)

要创建values-large目录,请执行与上面相同的步骤,但在本例中选择“大小”而不是“最小屏幕宽度”。选择大。目录名称将自动生成。

像以前一样右键单击该目录以创建bools.xml文件。