您的位置:首页 > 其它

动态显示和隐藏状态栏

2016-04-20 16:31 274 查看
原文地址:/article/7858209.html

小米Launcher有一个细节上的功能效果:在长按桌面应用图标时,会隐藏状态栏,然后在状态栏原有的布局上显示卸载或删除的操作栏。放手后,操作栏隐藏,状态栏显示出来。也就是说,这个过程是涉及到对状态栏的动态操作的。

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

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: