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;
        }
    }
  • 相关阅读:
    js插件ztree使用
    asp.net错误页和asp.net mvc错误页设置
    C#实现Excel的导入导出
    ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
    iOS开发UI篇—UITabBarController简单介绍
    iOS开发UI篇—字典转模型
    iOS开发UI篇—从代码的逐步优化看MVC
    iOS开发UI篇—九宫格坐标计算
    iOS开发UI篇—transframe属性(形变)
    iOS开发UI篇—懒加载
  • 原文地址:https://www.cnblogs.com/hsx514/p/4140980.html
Copyright © 2011-2022 走看看