View類提供了setSystemUiVisibility和getSystemUiVisibility方法,這兩個方法實現對狀態欄的動態顯示或隱藏的操做,以及獲取狀態欄當前可見性。html
setSystemUiVisibility(int visibility)方法可傳入的實參爲:java
1. View.SYSTEM_UI_FLAG_VISIBLE:顯示狀態欄,Activity不全屏顯示(恢復到有狀態的正常狀況)。android
2. View.INVISIBLE:隱藏狀態欄,同時Activity會伸展全屏顯示。web
3. View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏顯示,且狀態欄被隱藏覆蓋掉。app
4. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏顯示,但狀態欄不會被隱藏覆蓋,狀態欄依然可見,Activity頂端佈局部分會被狀態遮住。ide
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_FULLSCREENthis
7. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION:隱藏虛擬按鍵(導航欄)。有些手機會用虛擬按鍵來代替物理按鍵。spa
8. View.SYSTEM_UI_FLAG_LOW_PROFILE:狀態欄顯示處於低能顯示狀態(low profile模式),狀態欄上一些圖標顯示會被隱藏。.net
下面將以一個demo來驗證view的setSystemUiVisibility(int visibility)方法實現動態操做狀態欄:
1.MainActivity代碼以下:
[java] view plain copy
package com.example.handlestatusbar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener
{
private RelativeLayout mRLayout;
private Button mBtn1, mBtn2, mBtn3, mBtn4, mBtn5, mBtn6, mBtn7, mBtn8;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRLayout = (RelativeLayout)findViewById(R.id.content);
mBtn1 = (Button)findViewById(R.id.btn1);
mBtn2 = (Button)findViewById(R.id.btn2);
mBtn3 = (Button)findViewById(R.id.btn3);
mBtn4 = (Button)findViewById(R.id.btn4);
mBtn5 = (Button)findViewById(R.id.btn5);
mBtn6 = (Button)findViewById(R.id.btn6);
mBtn7 = (Button)findViewById(R.id.btn7);
mBtn8 = (Button)findViewById(R.id.btn8);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
mBtn4.setOnClickListener(this);
mBtn5.setOnClickListener(this);
mBtn6.setOnClickListener(this);
mBtn7.setOnClickListener(this);
mBtn8.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.btn1:
//顯示狀態欄,Activity不全屏顯示(恢復到有狀態的正常狀況)
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
break;
case R.id.btn2:
//隱藏狀態欄,同時Activity會伸展全屏顯示
mRLayout.setSystemUiVisibility(View.INVISIBLE);
break;
case R.id.btn3:
//Activity全屏顯示,且狀態欄被隱藏覆蓋掉。
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
break;
case R.id.btn4:
//Activity全屏顯示,但狀態欄不會被隱藏覆蓋,狀態欄依然可見,Activity頂端佈局部分會被狀態遮住
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
break;
case R.id.btn5:
//同mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
break;
case R.id.btn6:
//同mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_LAYOUT_FLAGS);
break;
case R.id.btn7:
//隱藏虛擬按鍵(導航欄)
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
break;
case R.id.btn8:
//狀態欄顯示處於低能顯示狀態(low profile模式),狀態欄上一些圖標顯示會被隱藏。
mRLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
break;
}
}
}
2.佈局文件main.xml文件的代碼以下:
[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s1"/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s2"/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s3"/>
<Button
android:id="@+id/btn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s4"/>
<Button
android:id="@+id/btn5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s5"/>
<Button
android:id="@+id/btn6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s6"/>
<Button
android:id="@+id/btn7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s7"/>
<Button
android:id="@+id/btn8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s8"/>
</LinearLayout>
</RelativeLayout>
3.string.xml文件代碼以下:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HandleStatusBar</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="s1">SYSTEM_UI_FLAG_VISIBLE</string>
<string name="s2">INVISIBLE</string>
<string name="s3">SYSTEM_UI_FLAG_FULLSCREEN</string>
<string name="s4">SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN</string>
<string name="s5">SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION</string>
<string name="s6">SYSTEM_UI_LAYOUT_FLAGS</string>
<string name="s7">SYSTEM_UI_FLAG_HIDE_NAVIGATION</string>
<string name="s8">SYSTEM_UI_FLAG_LOW_PROFILE</string>
</resources>