我如何禁用景观模式的一些观点在我的Android应用程序?
当前回答
添加android:screenOrientation="portrait"到你想禁用横向模式的活动中。
其他回答
在你的manifest中添加android:screenOrientation="portrait"。如果您想为特定的活动应用模式,请在所需的活动标记中定义此模式。或者在应用程序标签中定义整个应用程序。比如:
<application
<!-- ... -->
android:screenOrientation="portrait"
</application>
如果你想禁用Android应用程序(或单个活动)的横向模式,你需要做的就是添加:
android:screenOrientation="portrait"到AndroidManifest.xml文件中的活动标签。
如:
<activity
android:name="YourActivityName"
android:icon="@drawable/ic_launcher"
android:label="Your App Name"
android:screenOrientation="portrait">
另一种方法:程序化方法。
如果您希望以编程方式完成此操作,即使用Java代码。您可以在不希望以横向模式显示的活动的Java类中添加以下代码。
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
将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.
只需在你的清单中添加这一行:
android:screenOrientation="portrait"
如:
<manifest
package="com.example.speedtest"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="ComparisionActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
在阅读这篇文章之前,我不知道AndroidManifest.xml文件开关,所以在我的应用程序中,我使用了这个代替:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed portrait orientation
推荐文章
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?