【问题标题】:Setting Landscape orientation in phones using setRequestedOrientation使用 setRequestedOrientation 在手机中设置横向方向
【发布时间】:2018-01-17 11:44:11
【问题描述】:

使用 setRequestedOrientation 在 Android 应用程序活动中设置横向时,活动首先以纵向出现,然后在手机中变为横向。 setRequestedOrientaiton 在 onCreate 生命周期方法中被调用。是否可以直接以横向启动活动。由于我们只需要手机上的活动处于横向模式,因此我们无法在 manifest.xml 文件中设置方向。 (在平板电脑中,活动应该支持任何方向)

【问题讨论】:

  • 您可以在清单文件中为每个活动定义 screenOrientation 属性

标签: android android-activity android-orientation


【解决方案1】:

您可以使用清单设置方向并从资源中获取值。

<activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="@integer/orientation" />

在您的 integers.xml 文件中设置如下值:

<integer name="orientation">0</integer> <!-- Force landscape mode -->

<integer name="orientation">1</integer> <!-- Force portrait mode -->

<integer name="orientation">2</integer> <!-- Use phone's orientation -->

只需为手机和平板电脑创建一个 integers.xml 文件。

【讨论】:

  • 在添加时收到警告说:- 从清单中引用的资源不能因配置而异(版本限定符除外,例如 -v21。)在 sw600dp、sw720dp 中发现变化我已在其中添加了 integers.xml values, valjues-sw600dp,values-sw720dp 取值为: 0 in values 10 in sw600dp and sw720dp.即 10 表示完整的传感器方向和横向另一个。
  • 另外,我可以运行该应用程序,但即使在我的三星 Galaxy Tab S 中,它也会以横向显示,方向锁定。
  • 此外,在 Android 6 中未观察到此行为,在 onCreate 中将方向设置为横向时,活动仅在横向中直接加载。即它不会变成纵向并返回横向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 2013-10-08
  • 2017-02-11
相关资源
最近更新 更多