2010-03-23 137 views
52

如何確保我的應用僅適用於垂直佈局?我試過android:screenOrientation="portrait"但這似乎並沒有伎倆Android - 僅限垂直佈局

+12

您應該接受答案。 – 2013-06-09 22:17:46

回答

144

你需要添加到所有你的活動不只一個。我認爲你瞭解這個設置是針對各種應用程序的,但事實並非如此。

<activity android:name=".MyActivity" 
      android:label="My Activity" 
      android:screenOrientation="portrait"> 

將聲明添加到AndroidManifest中的活動標籤中,以便爲每個只想呈現肖像的活動添加聲明。

+9

@teepusink你應該接受這個答案。 – 2012-09-03 22:33:09

2

android:configChanges="keyboardHidden|orientation"添加到活動中。

1

只是爲了確認Pentium10的答案。 我有一個類似的問題,並添加android:screenOrientation =「肖像」標籤做了我的伎倆。

1

android:orientation =「vertical」 把這個屬性放在佈局根目錄下,嘗試它可能有幫助。

10

您必須更改爲AndroidManifest.xml。

你必須插入每個活動:

android:configChanges = "orientation" 

android:screenOrientation = "portrait" 

用於例如爲:

<activity android:name=".YourActivityName" 
        android:label="@string/app_name" 
        android:configChanges = "orientation" 
        android:screenOrientation = "portrait"> 

這適用於單個活動。但似乎沒有應用範圍的設置在那裏。

17

如果你想要一些團體的活動,在縱向模式時僅被鎖定,比你可以選擇下一個辦法:

public abstract class BasePortraitActivity extends Activity { 

    @Override 
    protected final void onCreate(Bundle state) { 
     super.onCreate(state); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     performOnCreate(state); 
    } 

    protected abstract void performOnCreate(Bundle state); 

} 

而不僅僅是延長BasePortraitActivity在你需要它。或者只需添加setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);YourActivity.onCreate()即可。

4

在活動下您的清單補充一點:

<activity 
     android:name="com.zeus.MyProject" 
     android:screenOrientation="portrait" 
     > 
2

活動是阻止以防萬一要阻止一切,只重複這行代碼在他們的Activitys。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 

    // Here, talk to the java does not want that the user can rotate their activity. 

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

或打開您的「AndroidManisfest.xml」並添加縱向模式的行,如下所示。

 android:configChanges="orientation" 
     android:screenOrientation="portrait">