Android 如何将布局方向固定为垂直方向?

Android 如何将布局方向固定为垂直方向?,android,android-orientation,Android,Android Orientation,如何将布局方向固定为纵向,并且不允许在运行时从纵向更改为横向?在您的AndroidMainfest.xml文件中找到要锁定到给定旋转的活动的标记,并添加此属性: android:screenOrientation="portrait" 在清单文件中的活动参数中 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"

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

在您的
AndroidMainfest.xml
文件中找到要锁定到给定旋转的活动的标记,并添加此属性:

android:screenOrientation="portrait"

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

<?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>


android:screenOrientation=“纵向”

如果您想在运行时冻结方向,那么您可以实现以下功能:


我使用了一种类似的方法,效果非常好。

使用
setRequestedOrientation()
,如图所示:

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
以前

setContentView(R.layout.main);

如果要确定项目中某个活动的方向,必须打开
Manifest.xml
,并将其放入所需活动的参数部分(在关闭第一个标记
之前):

android:screenOrientation=“纵向”
如果您想要垂直固定方向


android:screenOrientation=“横向”
如果你想在你的
AndroidMainfest.xml
中使用水平固定方向,只需在你声明的活动中写下这个

如果您想在布局中垂直,请使用

android:screenOrientation="portrait"
如果你想在布局景观比使用

android:screenOrientation="landscape"

就我个人而言,我更喜欢这样,如果你想更改方向,这真的很方便,否则如果你只想将设备置于该方向,请使用XML。@AlekseyTimoshchenko你能描述一下它是如何工作的吗?@parvus,当我添加此属性时在清单中,根据答案,我仍然可以进行轮换。。。最终我在下一步中找到了解决方案,我添加了这一行
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u grait)
on
onCreate()
android:screenOrientation="landscape"