转载自:http://blog.csdn.net/xiangyong_1521/article/details/51333437
最近添加系统功能需要监听状态栏的隐藏与显示,小小研究了下结合资料做下总结:
首先我用这个WindowAttributesChanged方法来监听视频播放页面状态栏的隐藏与显示,通过打印getWindow().getAttributes().flags,获取页面属性,从而来判断状态栏的显示与隐藏,可正常监听!
- @Override
- ublic void onWindowAttributesChanged(LayoutParams params) {
- super.onWindowAttributesChanged(params);
- Log.e(”Even”,“屏幕状态”+getWindow().getAttributes().flags);
- if(getWindow().getAttributes().flags==8455424){
- Log.i(”TAG”,“全屏状态”);
- }else if(getWindow().getAttributes().flags==8454400){
- Log.i(”TAG”,“非全屏状态”);
- }
@Override
public void onWindowAttributesChanged(LayoutParams params) {
super.onWindowAttributesChanged(params);
Log.e("Even","屏幕状态"+getWindow().getAttributes().flags);
if(getWindow().getAttributes().flags==8455424){
Log.i("TAG","全屏状态");
}else if(getWindow().getAttributes().flags==8454400){
Log.i("TAG","非全屏状态");
}
}
但是很快发现其他的程序用这个方法监听都只会执行一次,毕竟感觉旁门左道!于是几近波折成功地使用OnSystemUiVisibilityChangeListener接口来监听!
- @Override
- public void onSystemUiVisibilityChange(int visibility) {
- if (visibility==View.SYSTEM_UI_FLAG_FULLSCREEN||visibility==View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) {
- Log.i(”TAG”,“全屏状态======”);
- }else{
- Log.i(”TAG”,“非全屏状态======”);
- }
- }
@Override
public void onSystemUiVisibilityChange(int visibility) {
if (visibility==View.SYSTEM_UI_FLAG_FULLSCREEN||visibility==View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) {
Log.i("TAG","全屏状态======");
}else{
Log.i("TAG","非全屏状态======");
}
}
之前由于未加setOnSystemUiVisibilityChangeListener(this);监听,几度放弃这个方法而停滞!我勒个去。。
SystemUiVisibility(int visibility)方法可传入的实参:
View.SYSTEM_UI_FLAG_VISIBLE:显示状态栏,Activity不全屏显示(恢复到有状态的正常情况)。
View.INVISIBLE:隐藏状态栏,同时Activity会伸展全屏显示。
View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏显示,且状态栏被隐藏覆盖掉。
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
View.SYSTEM_UI_LAYOUT_FLAGS:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION:隐藏虚拟按键(导航栏)。有些手机会用虚拟按键来代替物理按键。
View.SYSTEM_UI_FLAG_LOW_PROFILE:状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。
写到这里干脆把状态栏设置隐藏与显示的方法也整理下:
View类提供了setSystemUiVisibility和getSystemUiVisibility方法,这两个方法实现对状态栏的动态显示或隐藏的操作,以及获取状态栏当前可见性。
下面将以一个demo来验证view的setSystemUiVisibility(int visibility)方法实现动态操作状态栏:
1、maiActivity.java:
- 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;
- }
- }
- }
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:
- <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>
<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:
- <?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>
<?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>