我如何禁用景观模式的一些观点在我的Android应用程序?
当前回答
将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.
其他回答
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.co.nurture.bajajfinserv">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我们可以使用属性或android:screenOrientation来限制活动在纵向或横向模式。
如果我们在程序中有多个活动,那么我们可以自由地限制任何模式下的任何一个活动,它不会影响你不想要的其他活动。
您必须设置每个活动的方向。
<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文件中添加android:screenOrientation="portrait"。
例如:
<activity
android:name=".MapScreen"
android:screenOrientation="portrait"></activity>
如果您正在使用Xamarin c#,其中一些解决方案将无法工作。这是我发现的一个有效的解决方案。
[Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
上面的类工作得很好,类似于其他解决方案。此外,它不是全局适用的,需要放在每个活动头中。
把它写进你的载货单里。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortrait" />
方向将是纵向的,但如果用户的手机是上下颠倒的,它也会显示正确的方式。(这样你的屏幕就会旋转180度。)
如果活动在多窗口模式下运行,系统将忽略此属性。
更多:https://developer.android.com/guide/topics/manifest/activity-element
推荐文章
- 在Android SQLite中处理日期的最佳方法
- 读取Android APK的包名
- Android-Facebook应用程序的键散列
- 登出时,清除活动历史堆栈,防止“返回”按钮打开已登录的活动
- 如何改变标题的活动在安卓?
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标