• Android点击按钮实现全屏的代码


    package com.hsx.test;
    
    import java.lang.reflect.Field;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.WindowManager;
    import android.view.WindowManager.LayoutParams;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.tv_open_fullscreen).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    stateControl(true);
    
                    int stateHeight = getStateHeight();
                    Toast.makeText(MainActivity.this, "状态栏高度为:" + stateHeight, Toast.LENGTH_LONG).show();
                }
            });
    
            findViewById(R.id.tv_close_fullscreen).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    stateControl(false);
                }
            });
    
        }
    
        /**
         * 控制是否全屏
         * 
         * @param enable
         * @return
         * @author Doraemon
         * @time 2014年12月3日下午6:03:33
         */
        private void stateControl(boolean enable) {
            if (enable) {
                LayoutParams lp = getWindow().getAttributes();
                lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                getWindow().setAttributes(lp);
            } else {
                LayoutParams attr = getWindow().getAttributes();
                attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().setAttributes(attr);
            }
    
        }
    
        /**
         * 获取状态栏高度
         * 
         * @return
         * @return
         * @author Doraemon
         * @time 2014年12月3日下午6:03:45
         */
        private int getStateHeight() {
            Class<?> c = null;
            Object obj = null;
            Field field = null;
            int x = 0, sbar = 0;
            try {
                c = Class.forName("com.android.internal.R$dimen");
                obj = c.newInstance();
                field = c.getField("status_bar_height");
                x = Integer.parseInt(field.get(obj).toString());
                sbar = getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sbar;
        }
    }
  • 相关阅读:
    ruby之gem update --system 失败
    免费的视频资源
    java的数据类型
    函数parseQuery用于解析url查询参数
    使用闭包的方式实现一个累加函数 addNum
    正则表达式之捕获重用
    JavaScript正则表达式练习
    JavaScript的数组和字符串应用
    Sublime Text3.0的安装
    Oracle的集合运算符
  • 原文地址:https://www.cnblogs.com/hsx514/p/4140980.html
Copyright © 2020-2023  润新知