• 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>  
  • 相关阅读:
    uva 10099(最大生成树+搜索)
    Codeforces Round #218 (Div. 2) 解题报告
    CodeChef December Challenge 2013 解题报告
    Codeforces Round #217 (Div. 2) 解题报告
    uva 1423 (拓扑排序)
    UESTC 1307 windy数(数位DP)
    Codeforces Round #216 (Div. 2) 解题报告
    Codeforces Round #215 (Div. 2) 解题报告
    uva 10047(BFS)
    uva 10369(最小生成树)
  • 原文地址:https://www.cnblogs.com/androidsj/p/4527235.html
Copyright © 2020-2023  润新知