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

Android 鎖定螢幕方向與隱藏標題欄、狀態列的方法

編輯:關於 Android 程式設計

做遊戲或直式閱讀器時,常需要把畫面方向固定住,同時把系統的標題欄與狀態列藏起來換取更大顯示空間。以下把常見做法整理成對照表。

固定螢幕方向

最簡單的方式是在 AndroidManifest.xml 的 Activity 節點加上 android:screenOrientation="portrait"。要留意的是 portrait 是「直向」,landscape 才是「橫向」,這兩個字面上的誤會很常見。

隱藏標題欄的三種寫法

  • 程式碼方式:requestWindowFeature(Window.FEATURE_NO_TITLE);,注意必須寫在 setContentView() 之前,否則會拋出例外。
  • 在 AndroidManifest.xml 的 application 或 activity 加上 android:theme="@android:style/Theme.NoTitleBar"。
  • 自訂 style 後再套用,彈性最高。

隱藏狀態列(全螢幕)

程式碼寫法為:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

完整的 AndroidManifest.xml 範例

 <manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.android.jetboy"
android:versionCode="1"
android:versionName="1.0.0">

<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">

<activity
android:name=".JetBoy"
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>

</application>

<uses-sdk android:minSdkVersion="4"></uses-sdk>

<!--
<uses-library android:name="android.test.runner" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.android.jetboy"
android:functionalTest="true"
android:label="Jetboy Test All Runner"/>
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION"/>
-->
</manifest>

新專案的建議做法

Android 4.4 之後,全螢幕建議改用 View.setSystemUiVisibility() 搭配 SYSTEM_UI_FLAG_FULLSCREEN 等旗標,並監聽 onWindowFocusChanged() 隨時恢復,才能相容瀏覽器列自動隱藏等新式行為。

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