轉自:http://blog.csdn.net/michaelpp/article/details/7302308android
動態切換全屏和非全屏:app
package com.screen;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private boolean isFulllScreen = false;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isFulllScreen = !isFulllScreen;
if (isFulllScreen) {
button.setText(getResources().getText(R.string.exit_full_screen));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(params);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
} else {
button.setText(getResources().getText(R.string.full_screen));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(params);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
});
}
} ide
非動態的全屏設置:
在實際的應用程序開發中,咱們有時須要把 Activity 設置成全屏顯示,通常狀況下,能夠經過兩種方式來設置全屏顯示效果。其一,經過在代碼中能夠設置,其二,經過manifest配置文件來設置全屏。
其一:在代碼中設置(以下)
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配置文件中設置
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/android.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>
在相應的Activity中節點中添加屬性:android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 便可以設置某個Activity全屏顯示。若設置成 android:theme="@android:style/Theme.NoTitleBar" 便是隻是設置成無標題狀態。idea