【问题标题】:Force tablet to be in landscape mode强制平板电脑处于横向模式
【发布时间】:2014-10-27 14:48:44
【问题描述】:

当用户启动我的应用程序时,有没有办法强制平板电脑处于横向模式作为默认方向。有没有办法通过主题或类似的东西来做到这一点,所以用户在启动应用程序时看不到方向变化?

注意:对于手机,同一个应用(应该)处于纵向模式

现在我有一个布尔值文件夹 refs.xml(values 和 values-large),对于平板电脑和手机具有不同的值,但用户可以看到应用程序正在旋转 代码:

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

【问题讨论】:

  • 一般来说我不建议你这样做。强制一个特定的方向会破坏用户体验,而且我不是唯一一个觉得它非常烦人的人,以至于我不使用这样做的应用程序。为什么不为不同的方向创建布局,让用户也以他们想要的方式使用他们的设备?

标签: android android-activity android-fragments android-fragmentactivity android-orientation


【解决方案1】:
    if (isTablet(getApplicationContext())) {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    setContentView(R.layout.yourlayout);



**//this method for check having run in tablet or not??**

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

【讨论】:

    【解决方案2】:

    我认为您只需在 activity tag 中添加 screenOrientation manifest.xml 如下:

    <activity>
        android:screenOrientation="landscape"
    </activity>
    

    您的应用程序将以landscape 模式打开

    【讨论】:

      【解决方案3】:

      横向并不意味着这是平板电脑。您应该将isTablet 用于存储在res/values-sw600dp 中的XML 文件中的表。

      【讨论】:

      • 我在我的项目中使用了这种方法,效果很好。我还在 values-sw720 中添加了 isTablet。
      【解决方案4】:

      在您的清单中的活动标记下添加以下行...

                  android:screenOrientation="landscape"
      

      【讨论】:

        【解决方案5】:

        您应该在定义activitiesManifest 文件中使用android:screenOrientation="landscape"

        <activity
                    android:name="com.yourpackage.ActivityClassName"
                    android:label="@string/app_name"
                    android:screenOrientation="landscape" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
        
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-24
          • 2011-08-12
          • 1970-01-01
          • 1970-01-01
          • 2014-03-04
          • 2020-10-19
          • 2012-03-26
          相关资源
          最近更新 更多