我如何禁用景观模式的一些观点在我的Android应用程序?
当前回答
这里的很多答案都建议在AndroidManifest.xml文件中使用“portrait”。这似乎是一个很好的解决方案-但正如文档中所指出的那样,您正在挑出可能只有横屏的设备。你还强迫某些设备(在横屏下工作得最好)进入竖屏,而没有得到正确的方向。
我的建议是用“nosensor”代替。这将使设备使用其默认的首选方向,不会阻止谷歌Play上的任何购买/下载,并将确保传感器不会破坏你的游戏(在我的情况下是NDK)。
其他回答
将android:screenOrientation="portrait"添加到AndroidManifest.xml中的活动中。例如:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
由于这已经成为一个超级流行的答案,我感到非常内疚,因为强制肖像很少是它经常应用的问题的正确解决方案。 强制肖像的主要注意事项:
This does not absolve you of having to think about activity lifecycle events or properly saving/restoring state. There are plenty of things besides app rotation that can trigger an activity destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles and retainInstance fragments. Keep in mind that unlike the fairly uniform iPhone experience, there are some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."
所以大多数应用程序应该让手机传感器、软件和物理配置自己决定用户想要如何与你的应用程序交互。但是,如果你对你的用例中传感器方向的默认行为不满意,你可能仍然需要考虑一些情况:
If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using nosensor for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them). If you still need to force portrait for some reason, sensorPortrait may be better than portrait for Android 2.3 (Gingerbread) and later; this allows for upside-down portrait, which is quite common in tablet usage.
您必须设置每个活动的方向。
<activity
android:name="com.example.SplashScreen2"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.Registration"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.Verification"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.WelcomeAlmostDone"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.PasswordRegistration"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
这里的很多答案都建议在AndroidManifest.xml文件中使用“portrait”。这似乎是一个很好的解决方案-但正如文档中所指出的那样,您正在挑出可能只有横屏的设备。你还强迫某些设备(在横屏下工作得最好)进入竖屏,而没有得到正确的方向。
我的建议是用“nosensor”代替。这将使设备使用其默认的首选方向,不会阻止谷歌Play上的任何购买/下载,并将确保传感器不会破坏你的游戏(在我的情况下是NDK)。
把它写进你的载货单里。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortrait" />
方向将是纵向的,但如果用户的手机是上下颠倒的,它也会显示正确的方式。(这样你的屏幕就会旋转180度。)
如果活动在多窗口模式下运行,系统将忽略此属性。
更多:https://developer.android.com/guide/topics/manifest/activity-element
<android . . . >
. . .
<manifest . . . >
. . .
<application>
<activity
android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
</manifest>
</android>
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。