android提供了兩種方式來實現無標題列和全屏效果,即通過xml檔案聲明的方式或在程式中動態控制的方式。
android設定全屏方法
一、通過程式設定:
代碼如下 |
複製代碼 |
package com.hhh.changeimage; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class ChangeImage extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //無title requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams. FLAG_FULLSCREEN); setContentView(R.layout.main); } } |
註:無title和全屏段代碼必須在setContentView(R.layout.main) 之前,不然會報錯。
二、在設定檔裡修改(AndroidManifest.xml)
代碼如下 |
複製代碼 |
<activity android:name=".ChangeImage" 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> |
下面我們結合上面的執行個體作一個Android全屏設定及取消全屏設定
•1、//在onCreat方法中setContentView()之前插入
代碼如下 |
複製代碼 |
•requestWindowFeature(Window.FEATURE_NO_TITLE);//取消標題列 •getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , • WindowManager.LayoutParams. FLAG_FULLSCREEN);//全屏
|
•註:這種方法在啟動activity時會閃現狀態列之後再全屏
•2、在manifest裡面配置:<activity android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />只在當前Activity內顯示全屏
•<application android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />為整個應用配置全螢幕顯示
•3、/**
• * 全屏切換
• */
代碼如下 |
複製代碼 |
•public void fullScreenChange() { •SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(this); •boolean fullScreen = mPreferences.getBoolean("fullScreen", false); •WindowManager.LayoutParams attrs = getWindow().getAttributes(); •System.out.println("fullScreen的值:" + fullScreen); •if (fullScreen) { •attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); •getWindow().setAttributes(attrs); •//取消全屏設定 •getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); •mPreferences.edit().putBoolean("fullScreen", false).commit() ; •} else { •attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; •getWindow().setAttributes(attrs); •getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); •mPreferences.edit().putBoolean("fullScreen", true).commit(); •} •} |