android 动态显示和隐藏状态栏

View类提供了setSystemUiVisibility和getSystemUiVisibility方法,这两个方法实现对状态栏的动态显示或隐藏的操作,以及获取状态栏当前可见性。

   setSystemUiVisibility(int visibility)方法可传入的实参为:

    1. View.SYSTEM_UI_FLAG_VISIBLE:显示状态栏,Activity不全屏显示(恢复到有状态的正常情况)。

    2. View.INVISIBLE:隐藏状态栏,同时Activity会伸展全屏显示。

    3. View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏显示,且状态栏被隐藏覆盖掉。

    4. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。

    5. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

    6. View.SYSTEM_UI_LAYOUT_FLAGS:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

    7. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION:隐藏虚拟按键(导航栏)。有些手机会用虚拟按键来代替物理按键。

    8. View.SYSTEM_UI_FLAG_LOW_PROFILE:状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。


   下面将以一个demo来验证view的setSystemUiVisibility(int visibility)方法实现动态操作状态栏:

   1.MainActivity代码如下:

[java] view plain copy

  1. package com.example.handlestatusbar;  

  2.   

  3. import android.annotation.SuppressLint;  

  4. import android.app.Activity;  

  5. import android.os.Bundle;  

  6. import android.view.Menu;  

  7. import android.view.MenuItem;  

  8. import android.view.View;  

  9. import android.view.View.OnClickListener;  

  10. import android.widget.Button;  

  11. import android.widget.RelativeLayout;  

  12. import android.widget.Toast;  

  13.   

  14. @SuppressLint("NewApi")  

  15. public class MainActivity extends Activity implements OnClickListener  

  16. {  

  17.   

  18.     private RelativeLayout mRLayout;  

  19.     private Button mBtn1, mBtn2, mBtn3, mBtn4, mBtn5, mBtn6, mBtn7, mBtn8;  

  20.       

  21.     @Override  

  22.     protected void onCreate(Bundle savedInstanceState)   

  23.     {  

  24.         super.onCreate(savedInstanceState);  

  25.         setContentView(R.layout.main);  

  26.           

  27.         mRLayout = (RelativeLayout)findViewById(R.id.content);  

  28.         mBtn1 = (Button)findViewById(R.id.btn1);  

  29.         mBtn2 = (Button)findViewById(R.id.btn2);  

  30.         mBtn3 = (Button)findViewById(R.id.btn3);  

  31.         mBtn4 = (Button)findViewById(R.id.btn4);  

  32.         mBtn5 = (Button)findViewById(R.id.btn5);  

  33.         mBtn6 = (Button)findViewById(R.id.btn6);  

  34.         mBtn7 = (Button)findViewById(R.id.btn7);  

  35.         mBtn8 = (Button)findViewById(R.id.btn8);  

  36.           

  37.         mBtn1.setOnClickListener(this);  

  38.         mBtn2.setOnClickListener(this);  

  39.         mBtn3.setOnClickListener(this);  

  40.         mBtn4.setOnClickListener(this);  

  41.         mBtn5.setOnClickListener(this);  

  42.         mBtn6.setOnClickListener(this);  

  43.         mBtn7.setOnClickListener(this);  

  44.         mBtn8.setOnClickListener(this);       

  45.     }  

  46.       

  47.     @Override  

  48.     public void onClick(View v)  

  49.     {  

  50.         // TODO Auto-generated method stub  

  51.         switch (v.getId())   

  52.         {  

  53.         case R.id.btn1:  

  54.             //显示状态栏,Activity不全屏显示(恢复到有状态的正常情况)  

  55.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);      

  56.             break;  

  57.         case R.id.btn2:  

  58.             //隐藏状态栏,同时Activity会伸展全屏显示  

  59.             mRLayout.setSystemUiVisibility(View.INVISIBLE);  

  60.             break;  

  61.         case R.id.btn3:  

  62.             //Activity全屏显示,且状态栏被隐藏覆盖掉。  

  63.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);  

  64.             break;        

  65.         case R.id.btn4:  

  66.             //Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住  

  67.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);  

  68.             break;  

  69.               

  70.         case R.id.btn5:  

  71.             //同mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);  

  72.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);  

  73.             break;  

  74.         case R.id.btn6:  

  75.             //同mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);  

  76.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_LAYOUT_FLAGS);  

  77.             break;  

  78.         case R.id.btn7:  

  79.             //隐藏虚拟按键(导航栏)  

  80.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);  

  81.             break;  

  82.         case R.id.btn8:  

  83.             //状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。  

  84.             mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);  

  85.             break;  

  86.         }  

  87.     }  

  88.   

  89. }  

    2.布局文件main.xml文件的代码如下:

[html] view plain copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     xmlns:tools="http://schemas.android.com/tools"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     tools:context=".MainActivity"   

  6.     android:id="@+id/content">  

  7.   

  8.    <LinearLayout   

  9.        android:orientation="vertical"  

  10.        android:layout_width="match_parent"  

  11.        android:layout_height="match_parent">  

  12.         <Button   

  13.         android:id="@+id/btn1"  

  14.         android:layout_width="fill_parent"  

  15.         android:layout_height="wrap_content"  

  16.         android:text="@string/s1"/>  

  17.       

  18.     <Button   

  19.         android:id="@+id/btn2"  

  20.         android:layout_width="fill_parent"  

  21.         android:layout_height="wrap_content"  

  22.         android:text="@string/s2"/>  

  23.       

  24.     <Button   

  25.         android:id="@+id/btn3"  

  26.         android:layout_width="fill_parent"  

  27.         android:layout_height="wrap_content"  

  28.         android:text="@string/s3"/>  

  29.       

  30.     <Button   

  31.         android:id="@+id/btn4"  

  32.         android:layout_width="fill_parent"  

  33.         android:layout_height="wrap_content"  

  34.         android:text="@string/s4"/>  

  35.       

  36.     <Button   

  37.         android:id="@+id/btn5"  

  38.         android:layout_width="fill_parent"  

  39.         android:layout_height="wrap_content"  

  40.         android:text="@string/s5"/>  

  41.       

  42.     <Button   

  43.         android:id="@+id/btn6"  

  44.         android:layout_width="fill_parent"  

  45.         android:layout_height="wrap_content"  

  46.         android:text="@string/s6"/>  

  47.       

  48.     <Button   

  49.         android:id="@+id/btn7"  

  50.         android:layout_width="fill_parent"  

  51.         android:layout_height="wrap_content"  

  52.         android:text="@string/s7"/>  

  53.       

  54.     <Button   

  55.         android:id="@+id/btn8"  

  56.         android:layout_width="fill_parent"  

  57.         android:layout_height="wrap_content"  

  58.         android:text="@string/s8"/>  

  59.       

  60.    </LinearLayout>  

  61.      

  62.   

  63. </RelativeLayout>  

   3.string.xml文件代码如下:    

[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <resources>  

  3.   

  4.   

  5.   

  6.     <string name="app_name">HandleStatusBar</string>  

  7.   

  8.     <string name="hello_world">Hello world!</string>  

  9.   

  10.     <string name="menu_settings">Settings</string>  

  11.     <string name="s1">SYSTEM_UI_FLAG_VISIBLE</string>  

  12.     <string name="s2">INVISIBLE</string>  

  13.     <string name="s3">SYSTEM_UI_FLAG_FULLSCREEN</string>  

  14.     <string name="s4">SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN</string>  

  15.     <string name="s5">SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION</string>  

  16.     <string name="s6">SYSTEM_UI_LAYOUT_FLAGS</string>  

  17.     <string name="s7">SYSTEM_UI_FLAG_HIDE_NAVIGATION</string>  

  18.     <string name="s8">SYSTEM_UI_FLAG_LOW_PROFILE</string>  

  19.     

  20.   

  21. </resources> 


你可能感兴趣的:(android 动态显示和隐藏状态栏)