zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    C# 常用小点
    WebClient下载文件
    redis常见错误处理
    win2008R2环境配置
    C#程序员知识体系
    js中json的添加和指定位置的删除
    关于本博客的美化
    箭头函数中可改变this作用域,回调函数用箭头函数this指向page,自定义事件用箭头函数this指向undefined
    json_decode 和 json_encode 区别
    tp6中使用微信支付sdk
  • 原文地址:https://www.cnblogs.com/hsx514/p/4140980.html
Copyright © 2011-2022 走看看