如何在 Android 中禁用横向模式?
-
06-09-2019 - |
题
如何禁用 Android 应用程序中某些视图的横向模式?
解决方案
添加 android:screenOrientation="portrait"
到 AndroidManifest.xml 中的活动。例如:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
编辑:由于这已成为一个超级流行的答案,我感到非常内疚,因为强制肖像很少是解决其经常应用的问题的正确解决方案。
强制肖像的主要注意事项:
- 这并不能使您不必考虑活动生命周期事件或正确保存/恢复状态。除应用程序旋转以外,还有很多事情可以触发活动破坏/娱乐,包括不可避免的事情,例如多任务处理。没有捷径;学习使用捆绑包和
retainInstance
碎片。 - 请记住,与相当统一的 iPhone 体验不同,在某些设备上,纵向并不是明显流行的方向。当用户使用带有硬件键盘或游戏手柄(例如 Nvidia Shield)的设备时, Chromebook, , 在 可折叠设备, ,或在 三星DeX, ,强制纵向可能会使您的应用程序体验受到限制或造成巨大的可用性麻烦。如果您的应用程序没有强有力的用户体验参数,而这会导致支持其他方向的负面体验,那么您可能不应该强制横向。我说的是“这是一个收银机应用程序,适用于始终在固定硬件底座中使用的一种特定型号的平板电脑。”
因此,大多数应用程序应该让手机传感器、软件和物理配置自行决定用户希望如何与您的应用程序交互。不过,如果您对默认行为不满意,您可能仍然需要考虑一些情况 sensor
您的用例的方向:
- 如果您主要担心的是活动期间意外的方向变化,您认为设备的传感器和软件无法很好地应对(例如,在基于倾斜的游戏中),请考虑支持横向和纵向,但使用
nosensor
为导向。这迫使大多数平板电脑上横向显示,大多数手机上纵向显示,但我仍然不建议大多数“普通”应用程序这样做(一些用户只是喜欢在手机上输入横向软键盘,而许多平板电脑用户以纵向阅读 - 并且你应该让他们)。 - 如果你 仍然 由于某种原因需要强制肖像,
sensorPortrait
可能比portrait
适用于 Android 2.3+;这允许倒置肖像,这在平板电脑使用中很常见。
其他提示
我不知道AndroidManifest.xml
文件切换,直到阅读这篇文章,所以在我的应用我已经使用这个来代替:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed Portrait orientation
在这里你声明你这样的活动增加这个android:screenOrientation="portrait"
清单档案中的
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait" />
如果你想使用Java代码尝试做
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
您拨打setContentView
方法在onCreate()
您的活动之前。
希望这有助于和易于理解的所有...
这是很多在这里的答案是暗示在AndroidManifest.xml文件使用"portrait"
。这似乎是一个很好的解决方案 - 但如文档中提到的,你挑出可能只有山水设备。您也迫使某些设备(即工作最好在横向)进入肖像,没有得到正确的方向。
我的建议是使用"nosensor"
代替。这将使该设备使用其默认择优取向,不阻断谷歌播放任何购买/下载,并确保传感器不弄乱你(NDK,在我的情况)的比赛。
如果你想用户设置,
那么我建议setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
可以从设置菜单改变设置。
我需要这个,因为我的时间必须符合什么是在屏幕上,并且旋转屏幕会破坏当前活动。
只需添加喜欢这一行你的清单
机器人:screenOrientation = “纵向”
<manifest
package="com.example.speedtestmeter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ComparisionActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="GaugeView"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="Resultactivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
活性的中的onCreate使用此()
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
就在您的活动代码,添加此属性。
android:screenOrientation="portrait"
如果你不想去通过活动更好的每个清单项添加定位的麻烦,创建一个类BaseActivity(继承“活动”或“AppCompatActivity”),将您的应用程序的每个活动被继承,而不是
:“活动”或“AppCompatActivity”和只需添加下面的代码段中的BaseActivity的@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// rest of your code......
}
您应该在AndroidManifest.xml改变android:screenOrientation="sensorPortrait"
添加到android:screenOrientation="portrait"
要禁用风景模式活动。
如果您希望禁用的 Landscape mode for your android app
(或者一个活动),所有你需要做的就是添加,
<强> 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);
我希望它可以帮助你。对于更多的细节,你可以点击这里查看进入链接描述此处
如何改变取向在一些视图的强>
而不是锁定整个活动的方向,你可以使用这个类来从您的任何视图的动态锁定方位务实: -
请您查看 风景的
OrientationUtils.lockOrientationLandscape(mActivity);
请您查看 人像的
OrientationUtils.lockOrientationPortrait(mActivity);
<强> 解锁 强>取向
OrientationUtils.unlockOrientation(mActivity);
取向的Util类
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Surface;
import android.view.WindowManager;
/* * This class is used to lock orientation of android app in nay android devices
*/
public class OrientationUtils {
private OrientationUtils() {
}
/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
/** Locks the device window in portrait mode. */
public static void lockOrientationPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/** Locks the device window in actual screen mode. */
public static void lockOrientation(Activity activity) {
final int orientation = activity.getResources().getConfiguration().orientation;
final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
.getRotation();
// Copied from Android docs, since we don't have these values in Froyo
// 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
// Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
if (!(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)) {
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}
/** Unlocks the device window in user defined screen mode. */
public static void unlockOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}
使用
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait"
您必须设置每个活动的取向。
<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>
添加内部的OnCreate类()方法
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
如果您使用的 Xamarin 的C#这些解决方案将无法工作。这是我发现工作溶液中。
[Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
上述类效果很好,类似于其它的解决方案,也没有全球适用的并且需要被放置在每个活动头。
您可以通过在你的manifest.xml写这迫使你的特别活动,始终保持在纵向模式
<activity android:name=".MainActivity"
android:screenOrientation="portrait"></activity>
您还可以强制您的活动在您的活动的onCreate()写入以下行留在postrait模式的方法
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
<android . . . >
. . .
<manifest . . . >
. . .
<application>
<activity android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
</manifest>
</android>
您可以为您的整个应用程序做到这一点,而不必让所有的活动扩展了一个公共基类。
关键是首先要确保你在项目中包含的应用程序的子类。在它的onCreate()被调用时您的应用程序第一次启动时,你注册一个ActivityLifecycleCallbacks对象(API等级14+)接收的活动生命周期事件通知。
这使您可以执行自己的代码,只要在你的应用程序的任何活动开始(或停止,或恢复,或其他)的机会。在这一点上,你可以在新创建的活动叫setRequestedOrientation()。
不要忘了添加应用程序:在您的清单文件名=“ MyApp的”
class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// register to be informed of activities starting up
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity,
Bundle savedInstanceState) {
// new activity created; force its orientation to portrait
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
....
});
}
}
在<apphome>/platform/android
目录中创建AndroidManifest.xml
(从所生成的一个复制它)。
然后android:screenOrientation=
“portrait
”添加到所有的活性元素。
<?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>
我们可以通过使用属性或机器人限制在纵向或横向模式中的活动:screenOrientation
如果我们在我的计划有一个以上的活动,那么你有自由限制活动的任何一个中的任何一个模式,它永远不会影响别人,你不想要的。
...添加android:screenOrientation="portrait"
中的的AndroidManifest.xml 文件。
<强>例如强>
<activity android:name=".MapScreen"
android:screenOrientation="portrait"></activity>
添加下面赞扬你的项目,
NPM安装
NPM我反应天然方位用储物柜
然后使用清单类等, React_Native(项目文件夹)/ 的机器人/应用/ SRC /主/ AndroidManifest.xml中强>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
感谢您!
在科特林一样可以使用以下
编程实现requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
这工作对我来说,尝试添加该代码AndroidManifest文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
....
....
</application>
无论是在清单类。
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait" />
或以编程
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
注意:你应该setContentView
方法之前调用此为onCreate()
您的活动
在希望能帮助别人,对在AndroidManifest.xml活动下列属性是你所需要的:
机器人:configChanges = “取向”
所以,完全活性节点:
<activity android:name="Activity1"
android:icon="@drawable/icon"
android:label="App Name"
android:configChanges="orientation">
如果您的与所述第一设备取向状态的活性,得到在onCreate
方法当前设备取向,然后修复它永远:
int deviceRotation = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
if(deviceRotation == Surface.ROTATION_0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_180)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_90)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(deviceRotation == Surface.ROTATION_270)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}