在documentation for the Android manifest中,有多种不同的方法来指定screenOrientation
landscape
sensorLandscape
在API 9中添加userLandscape
在API 18中添加我如何才能指定userLandscape
,但在较早版本的Android上,它是否回到了sensorLandscape
,而在更旧的版本上,它又回到了landscape
?我在文档中找不到如何做到这一点。
发布于 2015-02-21 09:36:59
我认为在清单本身中没有实现回退机制的方法。
我建议您在清单中指定{ userLandscape、sensorLandscape、拓朴}之一。然后,在运行时检查版本并即兴发挥。
比如说,你决定在清单中和android:screenOrientation="userLandscape"
一起去。
在您的活动的onCreate(Bundle)
中,在设置内容之前:
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);
希望有人能想出比这更好的解决方案。这看上去很残忍。
编辑:
尝试了一种不同的方法--据我所知(我在这里可能是错的),枚举(如userLandscape
、sensorLandscape
等)不会改变值。目前的情况是:
<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
,如:
<!-- `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
。
<!-- 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
<integer name="customScreenOrientation">?android:attr/screenOrientation</integer>
您的活动标签如下所示:
<activity
....
android:theme="@style/AppTheme"
android:screenOrientation="@integer/customScreenOrientation"/>
与第二种方法相比,这种方法的优点是我们可以使用枚举来代替硬编码的值。同样,如果枚举值是固定的,这两种方法是等价的.如果它们确实改变了,第二种方法将失败,而第三种方法将继续下去。
https://stackoverflow.com/questions/28575427
复制相似问题