Android Orientation Portrait layout only doesnt work

时间:2018-02-03 09:41:11

标签: android android-layout orientation portrait

Sorry for this million times repeated answer, (Im a newbe in Andoid), but I cannot achieve to stay layout in Portrait orientation only. I've tried to set it with recommended

android:screenOrientation="portrait"

in Mainfest.xml (for Activity, (have just one)), but it doesn't work. Anyone knows why ?

Below is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.leo.vj">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name_2"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
           android:label="@string/app_name"
           android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

5 个答案:

答案 0 :(得分:1)

It seems you declared your main activity wrongly.Try this

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name_2"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
   android:label="@string/app_name"
   android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>
<meta-data
    android:name="preloaded_fonts"
    android:resource="@array/preloaded_fonts" />

答案 1 :(得分:0)

You missed configChanges. In the manifest, set this for all your activities:

<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>

答案 2 :(得分:0)

The Problem is there is an extra '>' in end in declaration

    <activity 
             android:name = ".MainActivity">
             android:screenOrientation = "portrait">

Change the Activity Declaration in to :

<activity 
   android:name=".MainActivity"
       android:label="@string/app_name"
       android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <categoryandroid:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

答案 3 :(得分:0)

The above solution will work. You have missed the config changes.. If you want to acheive through Code.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

答案 4 :(得分:0)

Attribute name mismatch screenOrientation is the correct one

Please see below

android:screenOrientation="portrait"