在實際的應用程式開發中,我們有時需要把 Activity 設定成全螢幕顯示,一般情況下,可以通過兩種方式來設定全螢幕顯示效果:
其一,通過在代碼中可以設定,
其二,通過manifest設定檔來設定全屏。
其一:在代碼onCreate裡面setContentView之前設定(如下)
view plaincopy to clipboardprint?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消標題
requestWindowFeature(Window.FEATURE_NO_TITLE);
//取消狀態列
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
但要注意的是:在代碼中設定的話,設定無標題和設定全屏的兩段代碼要放置在 setContentView(R.layout.main)(介面渲染,完成了再全屏是不行的)這段代碼的前面。要不然會報錯。
其二:在manifest設定檔中設定
①在res/values 目錄建立個theme.xml檔案(用來放樣式)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- name 是Style的名稱,parent 繼承那個父類樣式 -->
<style name="theme_fullScreen" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item> <!-- 設定無標題 -->
<item name="android:windowFullscreen">?android:windowNoTitle</item> <!-- 是否填充慢螢幕,引用android:windowNoTitle 的值 ?android:windowNoTitle,取決於android:windowNoTitle的值-->
</style>
</resources>
②<activity android:name=".login.LoginActivity" android:theme="@style/theme_fullScreen"/>
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andyidea"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".login.LoginActivity"
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>
</manifest>
只去程式標題列
第三種:這種在一般的應用中不常用,就是在res/values目錄下面建立一個style.xml的檔案例如:
<?xml version="1.0" encoding="UTF-8" ?> <resources><style name="theme_notitle"><item name="android:windowNoTitle">true</item></style></resources>
這樣,我們就自訂了一個style,就相當於一個主題,然後在AndroidManifest.xml檔案中定義
<application android:icon="@drawable/icon" android:label="@string/app_name"android:theme="@style/theme_notitle">
這樣也可以達到去掉標題列的效果