- 在 AndroidManifest.xml 中設定 Theme.NoTitleBar.Fullscreen。
- 在 Java 檔案中透過 requestWindowFeature + WindowManager.LayoutParams.FLAG_FULLSCREEN 或者 setTheme( ) 方法設定。
在 AndroidManifest.xml 檔案中設定, 假如你要讓你所有程式中的 Activity 都是 Full Screen 你可以加在 Application tag 下,而如果只要單純設定一個 Activity 則加入到你想要設定的 Activity tag 下。範例如下所示:
- 套用整個程式的 Activity
<application ... android:theme= "@android:style/Theme.NoTitleBar.Fullscreen" > </application>
- 只套用在所設定的 Activity
<activity android:theme= "@android:style/Theme.NoTitleBar.Fullscreen" > ... </activity>
在 java 檔案中設定,你可以將下面的程式碼加到 onCreate( ) method 之中
requestWindowFeature(Window.FEATURE_NO_TITLE);
//fullscreen
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
或者
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
完整的程式碼如下:
package com.example.fullscreendemo; import android.os.Bundle; import android.view.Gravity; import android.view.Window; import android.view.WindowManager; import android.widget.LinearLayout; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set Full screen in Java file method 1 //turn off the window's title bar //requestWindowFeature(Window.FEATURE_NO_TITLE); // fullscreen //this.getWindow().setFlags(
// WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN);
LinearLayout ll =
new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setGravity(/*Gravity.CENTER*/Gravity.TOP);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(lp);
ll.setBackgroundResource(
R.drawable.background2);
// Set Full screen in Java file method 2
setTheme(
android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(ll); }
}
另外,我發現透過程式碼設定 Full Screen,在 emulator 上面執行,會先看到原本含有 title bar的 Activity,然後才淡出變成 Full Screen (如下圖),而透過 AndroidManifest.xml 檔案設定則沒有這個問題,所以想要在一開始就設定 Full Screen 的 Activity,還是透過 AndroidManifest.xml 檔案設定比較理想。
範例的結果:
參考鏈結
- how to set full screen layout for all activities
- android:theme=“@android:style/Theme.NoTitleBar.Fullscreen” works on Application level but not at the activity level. Any clue?
- How to remove the black space between the tittle bar and the first element of a LinearLayout?
- 【Android开发基础】应用界面主题Theme使用方法
沒有留言:
張貼留言