Android強制橫豎屏

來源:互聯網
上載者:User

全屏
在Activity的onCreate方法中的setContentView(myview)調用之前添加下面代碼
requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    WindowManager.LayoutParams.FLAG_FULLSCREEN);//設定全屏  橫屏


按照下面程式碼範例修改Activity的onResume方法

@Override  protected void onResume() {  
/**    * 設定為橫屏    */  
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){   
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
}  
super.onResume(); 
}  或者在設定檔中對Activity節點添加android:screenOrientation屬性(landscape是橫向,portrait是縱向)

android:launchMode="singleTask" android:screenOrientation="portrait">  要設定成豎屏設定成 SCREEN_ORIENTATION_PORTRAIT


還要說明一點:每個activity都有這個屬性screenOrientation,每個activity都需要設定,可以設定為豎屏(portrait),也可以設定為無重力感應(nosensor)。

要讓程式介面保持一個方向,不隨手機方向轉動而變化的處理辦法:
 
在AndroidManifest.xml裡面配置一下就可以了。加入這一行android:screenOrientation="landscape"。
例如(landscape是橫向,portrait是縱向):

Java代碼:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ray.linkit"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  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>
                <activity android:name=".GamePlay"
                android:screenOrientation="portrait"></activity>
                <activity android:name=".OptionView"
                android:screenOrientation="portrait"></activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>


另外,android中每次螢幕的切換動會重啟Activity,所以應該在Activity銷毀前儲存當前活動的狀態,在Activity再次Create的時候載入配置,那樣,進行中的遊戲就不會自動重啟了!

有的程式適合從豎屏切換到橫屏,或者反過來,這個時候怎麼辦呢?可以在配置Activity的地方進行如下的配置android:screenOrientation="portrait"。這樣就可以保證是豎屏總是豎屏了,或者landscape橫向。

而有的程式是適合橫豎屏切換的。如何處理呢?首先要在配置Activity的時候進行如下的配置:android:configChanges="keyboardHidden|orientation",另外需要重寫Activity的onConfigurationChanged方法。實現方式如下,不需要做太多的內容:

@Override
        public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
                if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                        // land do nothing is ok
                } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        // port do nothing is ok
                }
        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.