代码小疯子

你眼睛看到的不一定是真的

Android动态显示和隐藏状态栏

简述:

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

setSystemUiVisibility方法传入的实参分析:

   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模式),状态栏上一些图标显示会被隐藏。





相关代码:

package com.administrator.statubarapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        assignViews();


        setOnClicks();
    }

    private void setOnClicks() {
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mButton3.setOnClickListener(this);
        mButton4.setOnClickListener(this);
        mButton5.setOnClickListener(this);
        mButton6.setOnClickListener(this);
        mButton7.setOnClickListener(this);
    }

    private LinearLayout mMain;
    private TextView mTextview;
    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private Button mButton5;
    private Button mButton6;
    private Button mButton7;

    private void assignViews() {
        mMain = (LinearLayout) findViewById(R.id.main);
        mTextview = (TextView) findViewById(R.id.textview);
        mButton1 = (Button) findViewById(R.id.button1);
        mButton2 = (Button) findViewById(R.id.button2);
        mButton3 = (Button) findViewById(R.id.button3);
        mButton4 = (Button) findViewById(R.id.button4);
        mButton5 = (Button) findViewById(R.id.button5);
        mButton6 = (Button) findViewById(R.id.button6);
        mButton7 = (Button) findViewById(R.id.button7);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                //Activity全屏显示,且状态栏被隐藏覆盖掉
                mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
                mTextview.setText("Activity全屏显示,且状态栏被隐藏覆盖掉\nView.SYSTEM_UI_FLAG_FULLSCREEN");
                break;

            case R.id.button2:
                mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                mTextview.setText("恢复到有状态的正常情况\nView.SYSTEM_UI_FLAG_VISIBLE");
                break;
            case R.id.button3:
                mMain.setSystemUiVisibility(View.INVISIBLE);
                mTextview.setText("//隐藏状态栏,同时Activity会伸展全屏显示\nView.INVISIBLE");
                break;
            case R.id.button4:
                mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                mTextview.setText("Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮\nView" +
                        ".SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN \n View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION");
                break;
            case R.id.button5:
                mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                mTextview.setText("Activity全屏显示,状态栏透明\nView.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION");
                break;
            case R.id.button6:
                mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                mTextview.setText("隐藏虚拟按键\nView.SYSTEM_UI_FLAG_HIDE_NAVIGATION");
                break;
            case R.id.button7:
                mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                mTextview.setText("状态栏低能显示,有一些会被隐藏\nView.SYSTEM_UI_FLAG_LOW_PROFILE");
                break;

                        default:
                break;
        }
    }
}





阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zl18603543572/article/details/49828375
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭