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

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

<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);
}

但是,还是不走运。


当前回答

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

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

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

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

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

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

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

导入如下:

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

这肯定有用。

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

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