我试图为我的应用程序强制“纵向”模式,因为我的应用程序绝对不是为“横向”模式设计的。

在阅读了一些论坛后,我在我的清单文件中添加了这些行:

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

但它不能在我的设备(HTC Desire)上使用。它从“纵向”切换到“横向”,忽略清单文件中的行。

在更多的论坛阅读后,我试图在我的清单文件中添加以下内容:

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

这个函数在activity类中

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

但是,还是不走运。


不要将定向应用到应用程序元素,相反,您应该将属性应用到活动元素,并且您还必须按照下面所述设置configChanges。

例子:

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

这应用于清单文件AndroidManifest.xml。

我认为android:screenOrientation="portrait"可以用于个人活动。所以在<activity>标签中使用该属性,如下所示:

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

我认为你要添加android:configChanges="定向|keyboardHidden"到你的活动?否则,该活动将在config-change上重新启动。onConfigurationChanged不会被调用,只有onCreate被调用

请注意,

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

添加到清单文件中—其中定义了活动。

我在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">

我把它改成了(刚刚添加了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">

这为我解决了问题。

补充一点:我最近更新了一个应用程序,之前的应用程序可以在横向和纵向模式下工作,我希望更新的版本可以在纵向模式下工作,所以我添加了

android:screenOrientation="portrait"

到相应的活动,当我测试更新时,它崩溃了。然后我补充道

android:configChanges="orientation|keyboardHidden"

也是,而且很管用。

根据Android的文档,你也应该经常包括screenSize作为可能的配置更改。

android:configChanges="orientation|screenSize"

如果应用程序的目标是API级别13或更高(由 minSdkVersion和targetSdkVersion属性),那么您也应该 声明"screenSize"配置,因为当a 设备在纵向和横向方向之间切换。

此外,如果你都包括值keyboardHidden在你的例子,你不应该也考虑locale, mcc, fontScale,键盘和其他?

如果你有很多像我这样的活动,在你的应用程序中,或者如果你不想在manifest中为每个活动标签输入代码,你可以这样做。

在你的Application Base类中,你会得到一个生命周期回调

基本上,在每个活动中,当在应用程序类中创建时发生的事情是在这里被触发的代码。

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) {

            }
        });
}

我希望这能有所帮助。

简单的回答:不要这样做。

重新设计你的应用程序,使它可以在纵向和横向模式下运行。没有什么UI不能同时适用于纵向和横向;只有懒惰或缺乏想象力的开发者。

原因很简单。你希望你的应用能够被尽可能多不同设备上的用户所使用。通过强制使用特定的屏幕方向,你会阻止应用程序在不支持该方向的设备上运行,你会挫败和疏远那些喜欢不同方向的潜在客户。

例子:你将应用设计为强制竖屏模式。客户在二合一设备上下载应用程序,他们主要在横向模式下使用。 结果1:你的应用程序无法使用,或者你的客户被迫将他们的设备卸下,旋转它,并以他们不熟悉或不舒服的方向使用它。 结果2:用户对应用的非直观设计感到沮丧,并寻找替代方案或完全抛弃应用。

我现在正在与一款应用程序进行斗争,作为消费者和开发者,我讨厌它。尽管这个应用程序很有用,它提供的功能非常棒,但我非常讨厌这个应用程序,因为它迫使我使用一种与我使用设备的其他方式相反的方向。

你肯定不希望用户讨厌你的应用。


我知道这并不能直接回答问题,所以我想为那些好奇的人更详细地解释一下。

有一种趋势是,开发人员很擅长写代码,但很不擅长设计。这个问题,虽然听起来像一个代码问题,提问者当然觉得这是一个代码问题,但实际上是一个设计问题。

这个问题实际上是“我应该在应用程序中锁定屏幕方向吗?”提问者选择将UI设计为只在纵向模式下才能正常工作和看起来很好。我怀疑这是为了节省开发时间,或者是因为应用的工作流程特别有利于纵向布局(游戏邦注:这在手机游戏中很常见)。但这些原因忽略了所有真正重要的因素,激励适当的设计。

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.) 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.) 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. 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.

还有一种趋势是开发人员专注于实现特定的解决方案(例如锁定显示方向),并且这个站点上的大多数开发人员都非常乐意帮助实现该解决方案,而不会质疑这是否是问题的最佳解决方案。

锁定屏幕方向在UI设计中相当于实现了一个do-while循环。你真的想这么做吗,还是有更好的选择?

不要强迫你的应用进入单一的显示模式。投入额外的时间和精力让它有响应。

设置强制纵向或横向模式,分别添加线条。

导入如下:

import android.content.pm.ActivityInfo;

在setContentView(r.b ayout.activity_main)上面添加下面一行;

肖像:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//Set Portrait

Landscap:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Set Landscape

这肯定有用。

如果您希望在调试和发布版本中支持不同的方向,请这样写(参见https://developer.android.com/studio/build/gradle-tips#share-properties-with-the-manifest)。

在构建。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"]
        }
    }
}

然后在AndroidManifest中,你可以在任何活动中使用这个变量“orientation”:

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

你可以添加android:configChanges:

manifest占位符= [configChanges: "", orientation: "fullSensor"] in debug and manifest占位符= [configChanges: "keyboardHidden|orientation|screenSize", orientation: "portrait"] in release,

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

要让整个应用运行在完全竖屏模式或完全横屏模式,你需要做一件非常简单的事情。

转到应用程序的manifest文件夹 查看定义了intent-filter的活动。它基本上是应用启动时打开的第一个活动。 在这个活动中添加android:screenOrientation="portrait"

这将使你的整个应用程序运行在纵向模式,或者你也可以设置它运行在横向模式。