Android隱藏標題欄和狀態欄

一、隱藏標題欄

總體來說,Android隱藏標題欄分兩種方式:onCreate中使用代碼隱藏  和  AndroidMainfest 定義Activity主題隱藏 ;

Activity繼承自Activity時

onCreate-->setContentView方法前加入以下代碼:

requestWindowFeature(Window.FEATURE_NO_TITLE);  (也可用下方第二種方式)

Activity繼承自AppCompatActivity時

第一種方式:onCreate方法中隱藏

import android.support.v7.app.ActionBar;

ActionBar actionBar = getSupportActionBar();

if (actionBar != null) {

    actionBar.hide();

}

第二種方式:AndroidManifest文件中改變Activity的主題(theme) (這種方式,繼承自Activity或者AppCompatActivity都可用)

在res--->values--->styles--->中加入以下代碼:

<style name="MainStyle" parent="Theme.AppCompat.Light.NoActionBar"/>

然後在需要隱藏標題欄的activity標籤聲明中,加入以下代碼即可;

android:theme="@style/MainStyle"


二、隱藏狀態欄

第一步:AndroidManifest文件中設置主題爲NoActionBar 或者setContentView之前使用requestWindow設置無標題

第二步:在Activity的onCreate()方法中加入以下代碼,在setContentView之後加入以下代碼即可

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

第三步:在Activity的佈局文件根標籤下加入以下代碼

    android:fitsSystemWindows="true"

第四步:如果展示app啓動圖時,進入頁面後白屏閃爍纔可以看到啓動頁圖片,可以在當前Activity的Theme中加入以下代碼:

    <style name="LauncherTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
        <item name="android:windowBackground">@mipmap/ic_appstart</item>
    </style>

附:

Android:theme="@android:style/Theme.Dialog" 將一個Activity顯示爲能話框模式 
android:theme="@android:style/Theme.NoTitleBar" 不顯示應用程序標題欄 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不顯示應用程序標題欄,並全屏 
android:theme="Theme.Light" 背景爲白色 
android:theme="Theme.Light.NoTitleBar" 白色背景並無標題欄 
android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,無標題欄,全屏 
android:theme="Theme.Black" 背景黑色 
android:theme="Theme.Black.NoTitleBar" 黑色背景並無標題欄 
android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,無標題欄,全屏 
android:theme="Theme.Wallpaper" 用系統桌面爲應用程序背景 
android:theme="Theme.Wallpaper.NoTitleBar" 用系統桌面爲應用程序背景,且無標題欄 
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系統桌面爲應用程序背景,無標題欄,全屏 
android:theme="Translucent" 透明背景
android:theme="Theme.Translucent.NoTitleBar" 透明背景並無標題
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 透明背景並無標題,全屏
android:theme="Theme.Panel" 面板風格顯示
android:theme="Theme.Light.Panel" 平板風格顯示

 

 

 

 

 

 

 

 

     

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章