【问题标题】:Android Orientation Lock安卓方向锁
【发布时间】:2015-03-11 09:08:28
【问题描述】:

我的 Android 项目有一个小需求。

  1. 我的应用程序应该在平板电脑中运行Landscape,在Phone中运行Portrait
  2. 有什么方法可以在 Application 中锁定方向,而不是在 Activity 中设置。

【问题讨论】:

  • android:screenOrientation="portrait"
  • @RohitGoswami 如果我们锁定方向肖像,平板电脑的方向是什么
  • 您可以通过显示指标找到您设备的尺寸,然后您可以设置平板电脑和手机的方向

标签: android android-layout android-intent android-activity android-orientation


【解决方案1】:

您可以通过以下代码检查屏幕大小:

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

比设置方向:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

【讨论】:

  • yes )这是不同活动的重点 - 不同的配置。
  • 谢谢。无论如何要全局处理(即不是在每个活动中设置方向,而是为应用程序设置任何其他炒锅?)
【解决方案2】:

为了避免计算屏幕尺寸,您可以在尺寸中使用布尔值,例如

values-sw600dp/bools.xml -> <bool name="isTablet">true</bool>

values-sw720dp/bools.xml -> <bool name="isTablet">true</bool>

简单的values/bools -&gt; &lt;bool name="isTablet"&gt;false&lt;/bool&gt;

然后在activity中你可以简单的设置

boolean isTablet = getResources().getBoolean(R.bool.isTablet);
        if (isTablet) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onCreate(savedInstanceState);

请注意,这仅适用于API level 3.2 及以上。

【讨论】:

    【解决方案3】:
    if (getResources().getDisplayMetrics().widthPixels > getResources().getDisplayMetrics().heightPixels)
        {
            bLandscape = true;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            //initViews_Land();
        }
        else
        {
            bLandscape = false;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            //initViews_Port();
        }
    

    一种方法是在您的每个活动中使用此代码。

    另一种方法是为手机和平板电脑创建两个不同的项目。上传两个 APK(平板 APK 必须有更高的版本号)并在高级模式下激活它们。 Play Store 将根据正在安装的设备自动管理它

    Phone Manifest:所有活动都必须有 android:screenOrientation="sensorPortrait"

    ...
    <compatible-screens>
        <screen
            android:screenDensity="ldpi"
            android:screenSize="small" />
        <screen
            android:screenDensity="mdpi"
            android:screenSize="small" />
        <screen
            android:screenDensity="mdpi"
            android:screenSize="normal" />
        <screen
            android:screenDensity="hdpi"
            android:screenSize="normal" />
        <screen
            android:screenDensity="mdpi"
            android:screenSize="large" />
        <screen
            android:screenDensity="hdpi"
            android:screenSize="large" />
    </compatible-screens>
    
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"
        android:xlargeScreens="false" />
    ...
    

    Tablet Manifest:所有活动都必须有 android:screenOrientation="sensorLandscape"

    ...
    <compatible-screens>
        <screen
            android:screenDensity="xhdpi"
            android:screenSize="large" />
        <screen
            android:screenDensity="xhdpi"
            android:screenSize="xlarge" />
        <screen
            android:screenDensity="hdpi"
            android:screenSize="xlarge" />
        <screen
            android:screenDensity="mdpi"
            android:screenSize="xlarge" />
    </compatible-screens>
    
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="false"
        android:normalScreens="false"
        android:resizeable="false"
        android:smallScreens="false"
        android:xlargeScreens="true" />
    ...
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 2013-02-16
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多