首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >屏幕方向的显式回退

屏幕方向的显式回退
EN

Stack Overflow用户
提问于 2015-02-18 03:25:08
回答 1查看 2.1K关注 0票数 9

documentation for the Android manifest中,有多种不同的方法来指定screenOrientation

  • landscape
  • sensorLandscape在API 9中添加
  • userLandscape在API 18中添加

我如何才能指定userLandscape,但在较早版本的Android上,它是否回到了sensorLandscape,而在更旧的版本上,它又回到了landscape?我在文档中找不到如何做到这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-21 09:36:59

我认为在清单本身中没有实现回退机制的方法。

我建议您在清单中指定{ userLandscape、sensorLandscape、拓朴}之一。然后,在运行时检查版本并即兴发挥。

比如说,你决定在清单中和android:screenOrientation="userLandscape"一起去。

在您的活动的onCreate(Bundle)中,在设置内容之前:

代码语言:javascript
运行
复制
int sdkInt = Build.VERSION.SDK_INT;

// if we're running on some API level within [9, 18), use `sensorLandscape`
if (sdkInt >= Build.VERSION_CODES.GINGERBREAD /* 9 */ 
        && sdkInt < Build.VERSION_CODES.JELLY_BEAN_MR2 /* 18 */) {
    setRequestedOrientation(
            ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if (sdkInt < Build.VERSION_CODES.GINGERBREAD /* 9 */) {
    setRequestedOrientation(
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

// API 18 or above - handled in manifest

setContentView(R.layout.whatever);

希望有人能想出比这更好的解决方案。这看上去很残忍。

编辑:

尝试了一种不同的方法--据我所知(我在这里可能是错的),枚举(如userLandscapesensorLandscape等)不会改变值。目前的情况是:

代码语言:javascript
运行
复制
<attr name="screenOrientation">
    <enum name="unspecified" value="-1" />
    <enum name="landscape" value="0" />
    <enum name="portrait" value="1" />
    <enum name="user" value="2" />
    <enum name="behind" value="3" />
    <enum name="sensor" value="4" />
    <enum name="nosensor" value="5" />
    <enum name="sensorLandscape" value="6" />
    <enum name="sensorPortrait" value="7" />
    <enum name="reverseLandscape" value="8" />
    <enum name="reversePortrait" value="9" />
    <enum name="fullSensor" value="10" />
    <enum name="userLandscape" value="11" />
    <enum name="userPortrait" value="12" />
    <enum name="fullUser" value="13" />
    <enum name="locked" value="14" />
</attr>

因此,如果要定义一个integer,如:

代码语言:javascript
运行
复制
<!-- `0` for `landscape` -- defined in values/integers.xml -->
<integer name="customScreenOrientation">0</integer>

<!-- `6` for `sensorLandscape` -- defined in values-v9/integers.xml -->
<integer name="customScreenOrientation">6</integer>

<!-- `11` for `userLandscape` -- defined in values-v18/integers.xml -->
<integer name="customScreenOrientation">11</integer>

然后,您可以使用@integer/customScreenOrientation作为活动标记中android:screenOrientation的值。

不用说,这充其量不过是一次黑客攻击。如果有人能够确认screenOrientation的枚举值的稳定状态,这可能是一个可行的解决办法--比在多个活动中包含我先前建议中的代码更可取。

的另一个编辑:

我前面提到的第二种方法可以改进如下:

创建3个integers.xml文件,而不是多个styles.xml文件。我想你已经有一个了- values/styles.xml.创建values-v9/styles.xml & values-v18/styles.xml

代码语言:javascript
运行
复制
<!-- values/styles.xml -->
<style name="AppTheme" parent="@style/BaseTheme">
    <item name="android:screenOrientation">landscape</item>
</style>

<!-- values-v9/styles.xml -->
<style name="AppTheme" parent="@style/BaseTheme">
    <item name="android:screenOrientation">sensorLandscape</item>
</style>

<!-- values-v18/styles.xml -->
<style name="AppTheme" parent="@style/BaseTheme">
    <item name="android:screenOrientation">userLandscape</item>
</style>

接下来,创建values/integers.xml(一个文件)并定义一个整数customScreenOrientation

代码语言:javascript
运行
复制
<integer name="customScreenOrientation">?android:attr/screenOrientation</integer>

您的活动标签如下所示:

代码语言:javascript
运行
复制
<activity
    ....
    android:theme="@style/AppTheme"
    android:screenOrientation="@integer/customScreenOrientation"/>

与第二种方法相比,这种方法的优点是我们可以使用枚举来代替硬编码的值。同样,如果枚举值是固定的,这两种方法是等价的.如果它们确实改变了,第二种方法将失败,而第三种方法将继续下去。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28575427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档