You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AndroidManifest中screenOrientation标签已废弃,寻求替代解决方案

替代废弃的android:screenOrientation的竖屏设置方案

针对Android Studio Koala | 2024.1.1 Canary 5、Gradle插件8.3.1环境下,android:screenOrientation标签废弃的情况,以下是两种官方推荐的替代方案:

静态配置(清单文件中设置)

在<activity>标签内新增<layout>子节点,通过android:orientation指定竖屏,替代原来的android:screenOrientation="portrait":

<activity
    android:name=".YourTargetActivity"
    ...>
    <layout
        android:orientation="portrait" />
</activity>

动态配置(代码中设置)

如果需要在运行时动态切换为竖屏,直接操作Window的属性:

Kotlin 示例

window.attributes.apply {
    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
window.attributes = window.attributes

Java 示例

WindowManager.LayoutParams windowParams = getWindow().getAttributes();
windowParams.orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
getWindow().setAttributes(windowParams);

另外,若需避免屏幕旋转触发Activity重建,可在<activity>标签中添加android:configChanges="orientation|screenSize",配合上述配置使用。

内容的提问来源于stack exchange,提问作者Mudra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 23:42:03