在實際的應用程序開發中,我們有時需要把 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"/>
第二種方法
<?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>
只去程序標題欄 設置整個應用 no title
第三種:這種在一般的應用中不常用,就是在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">
這樣也可以達到去掉標題欄的效果
以上給大家總結了三種android去掉狀態欄的方法,希望本文所述能夠幫助到大家。
新聞熱點
疑難解答
圖片精選