码迷,mamicode.com
首页 > 移动开发 > 详细

(转) Android 全屏控制:动态切换全屏和非全屏

时间:2014-10-29 12:37:37      阅读:207      评论:0      收藏:0      [点我收藏+]

标签: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 全屏控制:动态切换全屏和非全屏

标签:android   style   blog   http   io   os   ar   sp   strong   

原文地址:http://www.cnblogs.com/ren-gh/p/4058974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!