Android 全屏控制:動態切換全屏和非全屏

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   os   ar   sp   strong   

轉自:http://blog.csdn.net/michaelpp/article/details/7302308

 

動態切換全屏和非全屏:

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);  
                }  
            }  
        });  
          
    }  
}  

 

 

非動態全屏設定:

在實際的應用程式開發中,我們有時需要把 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" 即是只是設定成無標題狀態。

在實際的應用程式開發中,我們有時需要把 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" 即是只是設定成無標題狀態。

(轉) Android 全屏控制:動態切換全屏和非全屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.