1. 程式人生 > >Android設定Activity(介面)為全屏顯示的兩種方法 xml和java程式

Android設定Activity(介面)為全屏顯示的兩種方法 xml和java程式

方法2在4.4等一些版本,執行會異常終止,推薦使用方法1.

全屏顯示程式碼見字型加粗部分

1. 方法1:AndroidManifest.xml 裡,Activity的 android:theme  指定為" @android :style/Theme.NoTitleBar.Fullscreen"

示例:  
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme=" @android :style/Theme.NoTitleBar.Fullscreen"

>
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

 2. 方法2: 在onCreate()裡指定No title
要加入:
 
      /*set it to be no title*/
      requestWindowFeature(Window.FEATURE_NO_TITLE);
       /*set it to be full screen*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 

/*   //也可以如下格式
      /*set it to be no title*/
     this. requestWindowFeature(Window.FEATURE_NO_TITLE);
       /*set it to be full screen*/ 
       this. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
*/


示例:
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*set it to be no title*/
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        
        /*set it to be full screen*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
                        
        setContentView(R.layout.activity_main);