android开发笔记之browser全屏功能的实现

     在android 的浏览器(browser)中,有一个设置全屏的功能,这二天,我在看浏览器(browser)的源码时,觉得这个其实完全能独立出来,并且对其它的app应用都有参考使用的价值。

    (1)MainActivity.java

package com.example.testfullscreen;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {
	
	private Button myButton = null;
	private Controller myController = null;
	
	private boolean isFullScreen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

	private void init() {
		// TODO Auto-generated method stub
		myButton = (Button) findViewById(R.id.myButton);
		myButton.setOnClickListener(this);
		myButton.setText("请点击我来测试全屏显示");
		
		myController = new Controller(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(view == myButton){
			isFullScreen = !isFullScreen;
			myController.setFullScreen(isFullScreen);			
		}
		
		if(isFullScreen){
			myButton.setText("现在是全屏显示");
		}else{
			myButton.setText("现在不是全屏显示");
		}	
	}
}

    (2)Controller.java

package com.example.testfullscreen;

import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;


public class Controller{
	
	private Activity mActivity;
	
	public Controller(Activity mActivity){
		this.mActivity = mActivity;
	}
	
	public void setFullScreen(boolean enabled){
		Window win = mActivity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        if (enabled) {
            winParams.flags |=  bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
	}
}

    (3)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

你可能感兴趣的:(android,browser,全屏)