zoukankan      html  css  js  c++  java
  • android 状态栏、标题栏、屏幕高度

    1.获取状态栏高度: 
    decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。 
    于是,我们就可以算出状态栏的高度了。 

    Rect frame = new Rect();  
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
    int statusBarHeight = frame.top;  

    2.获取标题栏高度: 
    getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,

    然后就可以知道标题栏的高度了。 

    1 int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
    2 //statusBarHeight是上面所求的状态栏的高度  
    3 int titleBarHeight = contentTop - statusBarHeight  

    3.获取屏幕高度 

    1 WindowManager windowManager = getWindowManager();  
    2 Display display = windowManager.getDefaultDisplay();  
    3 screenWidth = display.getWidth();  
    4 screenHeight = display.getHeight(); 
    1 DisplayMetrics dm = new DisplayMetrics();  
    2 this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity  
    3 screenWidth =dm.widthPixels;  
    4 screenHeight =dm.heightPixels;  

    以上两种方法在屏幕未显示的时候,还是处于0的状态,即要在setContentView调用之后才有效。 

    设置为无标题  requestWindowFeature(Window.FEATURE_NO_TITLE);  

    设置为全屏模式

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);    

    设置为横屏 setRequesteOrientation(ActivityInfo.SCREEN_ORIENTATION_LADSCAPE); 

    在开发中我们经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方法是在配置文件里改! 

     1 public void onCreate(Bundle savedInstanceState) {    
     2          super.onCreate(savedInstanceState);    
     3         //去除title      
     4        requestWindowFeature(Window.FEATURE_NO_TITLE);      
     5         //去掉Activity上面的状态栏  
     6         getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,        
     7                        WindowManager.LayoutParams. FLAG_FULLSCREEN);     
     8               
     9          setContentView(R.layout.main);    
    10 } 
     1 <application android:icon="@drawable/icon" android:label="@string/app_name">    
     2          <activity android:name=".OpenGl_Lesson1"    
     3                    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"    
     4                    android:label="@string/app_name">    
     5              <intent-filter>    
     6                  <action android:name="android.intent.action.MAIN" />    
     7                  <category android:name="android.intent.category.LAUNCHER" />    
     8              </intent-filter>    
     9          </activity>    
    10</application>  
  • 相关阅读:
    Ubuntu配置sublime text 3的c编译环境
    ORA-01078错误举例:SID的大写和小写错误
    linux下多进程的文件拷贝与进程相关的一些基础知识
    ASM(四) 利用Method 组件动态注入方法逻辑
    基于Redis的三种分布式爬虫策略
    Go语言并发编程总结
    POJ2406 Power Strings 【KMP】
    nyoj 会场安排问题
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    Java的String、StringBuffer和StringBuilder的区别
  • 原文地址:https://www.cnblogs.com/androidsj/p/4527235.html
Copyright © 2011-2022 走看看