我如何禁用景观模式的一些观点在我的Android应用程序?
当前回答
只需在活动标记中添加此属性。
android:screenOrientation="portrait"
其他回答
在你声明你的活动的manifest文件中添加android:screenOrientation="portrait"。是这样的:
<activity
android:name=".yourActivity"
....
android:screenOrientation="portrait" />
如果你想使用Java代码,试试:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
在onCreate()中调用setContentView方法之前。
如果你想禁用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);
把它写进你的载货单里。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortrait" />
方向将是纵向的,但如果用户的手机是上下颠倒的,它也会显示正确的方式。(这样你的屏幕就会旋转180度。)
如果活动在多窗口模式下运行,系统将忽略此属性。
更多:https://developer.android.com/guide/topics/manifest/activity-element
如果您正在使用Xamarin c#,其中一些解决方案将无法工作。这是我发现的一个有效的解决方案。
[Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.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.
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件