Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 應用全螢幕顯示的兩種實作方法

編輯:關於 Android 程式設計

在開發遊戲或沉浸式應用時,我們經常需要把應用設定為全螢幕、隱藏標題列。Android 提供了兩種做法:一種在程式碼中動態設定,另一種則在設定檔中靜態宣告。

方法一:在程式碼中設定

透過 requestWindowFeature 隱藏標題列,再用 setFlags 設定全螢幕旗標。要特別注意的是,這兩段程式碼必須放在 setContentView() 之前,否則不會生效:

public class OpenGl_Lesson1 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 無標題列
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN); // 全螢幕
        // 以上兩段程式碼必須設定在 setContentView() 之前
        setContentView(R.layout.main);
    }
}

方法二:在設定檔中設定

透過為 Activity 指定 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 主題,就能一次搞定無標題列與全螢幕,程式碼更為乾淨:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.android.tutor"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenGl_Lesson1"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:label="@string/app_name">
            <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>

相較之下,第二種方法無需在程式碼中寫入額外邏輯,結構更清晰,因此推薦使用設定檔的方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved