Android 程式開發:(八)處理螢幕方向的變化 —— 8.4控制Activity的顯示方向

來源:互聯網
上載者:User
偶爾地,你可能想要確保你的應用程式在某一個確定的方向上面顯示,橫屏模式或豎屏模式。例如,你可能會開發一款只在橫屏模式下面啟動並執行遊戲。在這種情況下,可以通過編寫代碼,強制把Activity的方向設定為橫向,這需要使用Activity類的setRequestOrientation()方法。[java] view plaincopyimport android.content.pm.ActivityInfo;  @Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);            // 設定為橫屏模式      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);        }  如果想要設定為豎屏模式,可以使用ActivityInfo.SCREEN_ORIENTATION_PORTRAIT常量。除了使用setRequestOrientation()方法,也可以在AndroidManifest.xml檔案中進行設定。[html] view plaincopy<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="net.learn2develop.Orientations"      android:versionCode="1"      android:versionName="1.0" >        <uses-sdk android:minSdkVersion="14" />        <application          android:icon="@drawable/ic_launcher"          android:label="@string/app_name" >          <activity              android:label="@string/app_name"              android:name=".OrientationsActivity"          <!-- 設定screenOrientation-->              android:screenOrientation="landscape" >              <intent-filter >                  <action android:name="android.intent.action.MAIN" />                    <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>      </application>    </manifest>  在之前的例子中,被包含的Activity被設定成了橫屏模式,同時阻止了Activity被銷毀。這是因為,Activity被設定成了固定的方向,當裝置的方向改變時,Activity不會被銷毀,onCreate()方法也就不會被調用。下面是android:screenOrientation屬性的另外兩個可取的值:portrait —— 豎屏模式sensor —— 依據重力測試器去判斷方向(預設值)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.