75

如何将布局方向固定为纵向并且不允许在运行时从纵向更改为横向?

4

7 回答 7

155

在您的AndroidMainfest.xml文件中找到您希望锁定到给定轮换的活动的标签,并添加此属性:

android:screenOrientation="portrait"
于 2011-04-18T11:25:22.463 回答
22

如图使用setRequestedOrientation()

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
于 2011-05-05T03:14:46.837 回答
16

在清单文件中的活动参数中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.statepermit" android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/stateheader" android:label="@string/app_name">
        <activity android:name=".statepermit" android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest>

安卓:screenOrientation="人像"

于 2011-04-18T11:28:14.050 回答
3
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

setContentView(R.layout.main);
于 2013-10-12T08:17:23.797 回答
3

如果你想在运行时冻结方向,那么你可以实现这个:

Android:暂时禁用活动中的方向更改

我使用了类似的方法,并且效果很好。

于 2011-04-18T19:35:59.467 回答
2

如果要在项目中修复一个 Activity 的方向,则必须打开Manifest.xml并放入所需 Activity 的参数部分(在关闭第一个标签之前 < activity…> ):

android:screenOrientation="portrait"如果你想要 VERTICAL 固定方向

android:screenOrientation="landscape"如果你想要水平固定方向

于 2014-11-18T15:28:45.333 回答
2

AndroidMainfest.xml你声明的活动中写下这个,

如果您想要垂直布局而不是使用

android:screenOrientation="portrait"

如果你想在布局横向而不是使用

android:screenOrientation="landscape"
于 2016-02-05T10:32:26.627 回答