简体   繁体   English

强制“纵向”定向模式

[英]Force "portrait" orientation mode

I'm trying to force the "portrait" mode for my application because my application is absolutely not designed for the "landscape" mode.我正在尝试为我的应用程序强制使用“纵向”模式,因为我的应用程序绝对不是为“横向”模式设计的。

After reading some forums, I added these lines in my manifest file:在阅读了一些论坛后,我在清单文件中添加了这些行:

<application 
  android:debuggable="true"
  android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:screenOrientation="portrait">

But it doesn't work on my device (HTC Desire).但它不适用于我的设备(HTC Desire)。 It switches from "portrait" lo "landscape", ignoring the lines from the manifest file.它从“纵向”切换到“横向”,忽略清单文件中的行。

After more forums reading, I tried to add this in my manifest file:在阅读更多论坛后,我尝试将其添加到我的清单文件中:

<application 
  android:debuggable="true"
  android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:configChanges="orientation"       
  android:screenOrientation="portrait">

and this function in my activity class:我的活动类中的这个函数:

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

But again, no luck.但是,再一次,没有运气。

Don't apply the orientation to the application element, instead you should apply the attribute to the activity element, and you must also set configChanges as noted below.不要将方向应用应用程序元素,而应该将属性应用到活动元素,并且还必须如下所述设置configChanges

Example:示例:

<activity
   android:screenOrientation="portrait"
   android:configChanges="orientation|keyboardHidden">
</activity>

This is applied in the manifest file AndroidManifest.xml .这应用于清单文件AndroidManifest.xml

Note that请注意

android:screenOrientation="portrait"     
android:configChanges="orientation|keyboardHidden"

is added in the manifest file - where the activity is defined.添加在清单文件中 - 活动被定义的地方。

If you are having a lot activity like mine, in your application Or if you dont want to enter the code for each activity tag in manifest you can do this .如果你有很多像我这样的活动,在你的应用程序中或者如果你不想在清单中输入每个活动标签的代码,你可以这样做。

in your Application Base class you will get a lifecycle callback在您的应用程序基类中,您将获得生命周期回调

so basically what happens in for each activity when creating the on create in Application Class get triggered here is the code ..所以基本上在应用程序类中创建 on create 时每个活动发生的事情在这里被触发是代码..

public class MyApplication extends Application{

@Override
    public void onCreate() {
        super.onCreate();  

  registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                activity.setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


// for each activity this function is called and so it is set to portrait mode


            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
}

i hope this helps.我希望这会有所帮助。

I think android:screenOrientation="portrait" can be used for individual activities.我认为android:screenOrientation="portrait"可用于个人活动。 So use that attribute in <activity> tag like :所以在<activity>标签中使用该属性,如:

<activity android:name=".<Activity Name>"
    android:label="@string/app_name" 
    android:screenOrientation="portrait">
   ...         
</activity>

Set force Portrait or Landscape mode , Add lines respectively.设置强制纵向或横向模式,分别添加线条。

Import below line:导入以下行:

import android.content.pm.ActivityInfo;

Add Below line just above setContentView(R.layout.activity_main);setContentView(R.layout.activity_main);上方添加下面的行setContentView(R.layout.activity_main);

For Portrait :对于肖像

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//Set Portrait

For Landscap :对于景观

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Set Landscape

This will definitely work.这肯定会奏效。

According to Android's documentation, you should also often include screenSize as a possible configuration change.根据 Android 的文档,您还应该经常包含screenSize作为可能的配置更改。

android:configChanges="orientation|screenSize"

If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.如果您的应用程序面向 API 级别 13 或更高级别(由 minSdkVersion 和 targetSdkVersion 属性声明),那么您还应该声明“screenSize”配置,因为当设备在纵向和横向之间切换时它也会发生变化。

Also, if you all include value keyboardHidden in your examples, shouldn't you then also consider locale , mcc , fontScale , keyboard and others?..另外,如果你们都在你的例子中包含值keyboardHidden ,那么你不应该同时考虑localemccfontScalekeyboard和其他吗?...

I had this line in my AndroidManifest.xml我的 AndroidManifest.xml 中有这一行

<activity 
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
    android:label="@string/app_name" android:name="Project Name"
    android:theme="@android:style/Theme.Black.NoTitleBar">

Which I changed to (just added android:screenOrientation="portrait" )我改为(刚刚添加android:screenOrientation="portrait"

<activity 
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
    android:label="@string/app_name" android:name="Project Name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar">

This fixed things for me.这对我来说是固定的。

Something to complement: I have updated an app recently, the previous was working in both landscape and portrait mode, and I want the updated version should work in portrait mode, so I added补充一点:我最近更新了一个应用程序,以前的应用程序可以在横向和纵向模式下运行,我希望更新后的版本应该在纵向模式下运行,所以我添加了

android:screenOrientation="portrait"

to the corresponding activity, and it just crashed when I tested the update.到相应的活动,当我测试更新时它刚刚崩溃。 Then I added然后我加了

android:configChanges="orientation|keyboardHidden"

too, and it works.也是,它的工作原理。

If you wish to support different orientations in debug and release builds, write so (see https://developer.android.com/studio/build/gradle-tips#share-properties-with-the-manifest ).如果您希望在debugrelease版本中支持不同的方向,请这么写(参见https://developer.android.com/studio/build/gradle-tips#share-properties-with-the-manifest )。

In build.gradle of your app folder write:在您的app文件夹的build.gradle中写入:

android {
    ...
    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // Creates a placeholder property to use in the manifest.
            manifestPlaceholders = [orientation: "fullSensor"]
        }
        release {
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // Creates a placeholder property to use in the manifest.
            manifestPlaceholders = [orientation: "portrait"]
        }
    }
}

Then in AndroidManifest you can use this variable "orientation" in any Activity :然后在AndroidManifest您可以在任何Activity使用此变量“方向”:

<activity
    android:name=".LoginActivity"
    android:screenOrientation="${orientation}" />

You can add android:configChanges :您可以添加android:configChanges

manifestPlaceholders = [configChanges: "", orientation: "fullSensor"] in debug and manifestPlaceholders = [configChanges: "keyboardHidden|orientation|screenSize", orientation: "portrait"] in release, manifestPlaceholders = [configChanges: "", orientation: "fullSensor"]在 debug 和manifestPlaceholders = [configChanges: "keyboardHidden|orientation|screenSize", orientation: "portrait"]在 release,

<activity
    android:name=".LoginActivity"
    android:configChanges="${configChanges}"
    android:screenOrientation="${orientation}" />

I think you want to add android:configChanges="orientation|keyboardHidden" to your activity?我认为您想将android:configChanges="orientation|keyboardHidden"到您的活动中? Otherwise the activity is restarted on config-change.否则,活动将在配置更改时重新启动。 The onConfigurationChanged would not be called then, only the onCreate然后不会调用onConfigurationChanged ,只会调用onCreate

To make your whole app run on complete Portrait mode or complete Landscape mode you have to do a very simple thing.为了让你的整个应用程序在完整的纵向模式或完整的横向模式下运行,你必须做一件非常简单的事情。

  1. Go to your app's manifest folder转到您应用的清单文件夹
  2. See the activity in which intent-filter is defined.查看定义了意图过滤器的活动。 It's basically the first activity which opens when your app is launched.它基本上是您的应用程序启动时打开的第一个活动。
  3. Inside this activity add android:screenOrientation="portrait"在此活动中添加android:screenOrientation="portrait"

This will make your whole app run on Portrait mode or you can also set it to run on Landscape mode.这将使您的整个应用程序在纵向模式下运行,或者您也可以将其设置为在横向模式下运行。

Short answer: Don't do it.简短的回答:不要这样做。

Redesign your app so that it can run in both portrait and landscape mode.重新设计您的应用程序,使其可以在纵向和横向模式下运行。 There is no such thing as a UI that can't be designed to work in both portrait and landscape;没有任何 UI 不能设计为同时适用于纵向和横向; only lazy or unimaginative developers.只有懒惰或缺乏想象力的开发人员。

The reason why is rather simple.原因比较简单。 You want your app to be usable by as wide an audience as possible on as many different devices as possible.您希望您的应用能够在尽可能多的不同设备上被尽可能多的受众使用。 By forcing a particular screen orientation, you prevent your app from running (usably) on devices that don't support that orientation and you frustrate and alienate potential customers who prefer a different orientation.通过强制特定的屏幕方向,您可以阻止您的应用程序在不支持该方向的设备上运行(可用),并且您会挫败和疏远喜欢不同方向的潜在客户。

Example: You design your app to force portrait mode.示例:您将应用程序设计为强制纵向模式。 A customer downloads the app on a 2-in-1 device which they use predominantly in landscape mode.客户在他们主要在横向模式下使用的二合一设备上下载该应用程序。
Consequence 1: Your app is unusable, or your customer is forced to undock their device, rotate it, and use it in an orientation that is not familiar or comfortable for them.后果 1:您的应用程序无法使用,或者您的客户被迫断开设备、旋转设备并以他们不熟悉或不舒服的方向使用它。
Consequence 2: The customer gets frustrated by your app's non-intuitive design and finds an alternative or ditches the app entirely.后果 2:客户对您的应用程序的非直观设计感到沮丧,并找到替代方案或完全放弃应用程序。

I'm fighting with this with an app right now and as a consumer and a developer, I hate it.我现在正在用一个应用程序来解决这个问题,作为消费者和开发者,我讨厌它。 As useful as the app is, as fantastic as the features are that it offers, I absolutely hate the app because it forces me to use an orientation that is counter to every other way that I use my device.与应用程序一样有用,与它提供的功能一样出色,我绝对讨厌该应用程序,因为它迫使我使用与我使用设备的所有其他方式相反的方向。

You don't want your customers to hate your app.您不希望您的客户讨厌您的应用。


I know this doesn't directly answer the question, so I want to explain it in a little more detail for those who are curious.我知道这并不能直接回答问题,所以我想为那些好奇的人更详细地解释一下。

There is a tendency for developers to be really good at writing code and really terrible at design.有一种趋势,开发人员非常擅长编写代码,而在设计方面却非常糟糕。 This question, though it sounds like a code question and the asker certainly feels like it's a code question, is really a design question.这个问题虽然听起来像一个代码问题,而且提问者肯定觉得这是一个代码问题,但实际上是一个设计问题。

The question really is "Should I lock the screen orientation in my app?"问题实际上是“我应该在我的应用程序中锁定屏幕方向吗?” The asker chose to design the UI to function and look good only in portrait mode.提问者选择将 UI 设计为仅在纵向模式下运行和看起来不错。 I suspect it was to save development time or because the app's workflow is particularly conducive to a portrait layout (common for mobile games).我怀疑这是为了节省开发时间,或者因为应用程序的工作流程特别有利于纵向布局(常见于手机游戏)。 But those reasons neglect all the real important factors that motivate proper design.但这些原因忽略了所有真正重要的因素,这些因素促使正确的设计。

  1. Customer engagement - you want your customers to feel pulled into your app, not pushed out of it.客户参与 - 您希望您的客户感觉被拉入您的应用程序,而不是被排除在外。 The app should transition smoothly from whatever your customer was doing prior to opening your app.该应用程序应该从您的客户在打开您的应用程序之前所做的任何事情顺利过渡。 (This is the reason most platforms have consistent design principles, so most apps look more or less alike though they don't have to.) (这就是大多数平台具有一致设计原则的原因,因此大多数应用程序看起来或多或少相似,尽管它们不必如此。)

  2. Customer response - you want your customers to react positively to your app.客户响应 - 您希望客户对您的应用做出积极响应。 They should enjoy using it.他们应该喜欢使用它。 Even if it's a payroll app for work, it should be a pleasure for them to open it and clock in. The app should save your customers time and reduce frustration over alternatives.即使它是一个用于工作的薪资应用程序,打开它并打卡对他们来说应该是一种乐趣。该应用程序应该为您的客户节省时间并减少对替代方案的挫败感。 (Apps that annoy users build resentment against your app which grows into resentment against your brand.) (惹恼用户的应用程序会对您的应用程序产生怨恨,进而对您的品牌产生怨恨。)

  3. Customer conversion - you want your customers to be able to quickly and easily move from browsing to interacting.客户转化——您希望您的客户能够快速轻松地从浏览转向互动。 This is the ultimate goal of any app, to convert impressions into revenue.这是任何应用程序的最终目标,将展示次数转化为收入。 (Apps that don't generate revenue are a waste of your time to build, from a business perspective.) (从业务角度来看,不产生收入的应用程序是在浪费您的时间来构建。)

A poorly designed UI reduces customer engagement and response which ultimately results in lower revenue.设计不当的用户界面会降低客户参与度和响应,最终导致收入降低。 In a mobile-centric world (and particularly on the subject of portrait/landscape display modes), this explains why responsive web design is such a big deal.在以移动为中心的世界中(尤其是在纵向/横向显示模式的主题上),这解释了为什么响应式网页设计如此重要。 Walmart Canada introduced responsive design on their website in November 2013 and saw a 20% increase in customer conversion. 加拿大沃尔玛于 2013 年 11 月在其网站上引入了响应式设计,客户转化率增加了20% O'Neill Clothing implemented responsive web design and revenue from customers using iOS devices increased 101.25% , and 591.42% from customers using Android devices .奥尼尔服装实施自适应设计,并使用iOS设备的客户收入同比增长101.25%,并使用Android设备591.42%客户

There is also a tendency for developers to focus intently on implementing a particular solution (such as locking display orientation), and most of the developers on this site will be all too glad to help implement that solution, without questioning whether that is even the best solution to the problem.开发人员也倾向于专注于实施特定的解决方案(例如锁定显示方向),并且本网站上的大多数开发人员都非常乐意帮助实施该解决方案,而不会质疑这是否是最好的问题的解决方案。

Locking your screen orientation is the UI design equivalent of implementing a do-while loop.锁定屏幕方向相当于实现 do-while 循环的 UI 设计。 Are you really sure you want to do it that way, or is there a better alternative?真的确定要那样做,还是有更好的选择?

Don't force your app into a single display mode.不要强迫您的应用进入单一显示模式。 Invest the extra time and effort to make it responsive.投入额外的时间和精力使其响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM