Android - Respond to UI visibility changes
发布日期:2021-05-06 23:02:44 浏览次数:28 分类:精选文章

本文共 5674 字,大约阅读时间需要 18 分钟。

文章目录

Respond to UI visibility changes

This lesson describes how to register a listener so that your app can get notified of system UI visibility changes. This is useful if you want to synchronize other parts of your UI with the hiding/showing of system bars.

Register a Listener

To get notified of system UI visibility changes, register an View.OnSystemUiVisibilityChangeListener to your view. This is typically the view you are using to control the navigation visibility.

For example, you could add this code to your activity’s onCreate() method:

View decorView = getWindow().getDecorView();decorView.setOnSystemUiVisibilityChangeListener        (new View.OnSystemUiVisibilityChangeListener() {       @Override    public void onSystemUiVisibilityChange(int visibility) {           // Note that system bars will only be "visible" if none of the        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {               // TODO: The system bars are visible. Make any desired            // adjustments to your UI, such as showing the action bar or            // other navigational controls.        } else {               // TODO: The system bars are NOT visible. Make any desired            // adjustments to your UI, such as hiding the action bar or            // other navigational controls.        }    }});

It’s generally good practice to keep your UI in sync with changes in system bar visibility. For example, you could use this listener to hide and show the action bar in concert with the status bar hiding and showing.


准备

IDE:

Android Studio 4.1.1Build #AI-201.8743.12.41.6953283, built on November 5, 2020Runtime version: 1.8.0_242-release-1644-b01 amd64VM: OpenJDK 64-Bit Server VM by JetBrains s.r.oWindows 10 10.0

Android Virtual Devices:

Name: Pixel_2_API_28CPU/ABI: Google Play Intel Atom (x86)Path: C:\Users\86188\.android\avd\Pixel_2_API_28.avdTarget: google_apis_playstore [Google Play] (API level 28)Skin: pixel_2SD Card: 512Mfastboot.chosenSnapshotFile: runtime.network.speed: fullhw.accelerometer: yeshw.device.name: pixel_2hw.lcd.width: 1080hw.initialOrientation: Portraitimage.androidVersion.api: 28tag.id: google_apis_playstorehw.mainKeys: nohw.camera.front: emulatedavd.ini.displayname: Pixel 2 API 28hw.gpu.mode: autohw.ramSize: 1536PlayStore.enabled: truefastboot.forceColdBoot: nohw.cpu.ncore: 4hw.keyboard: yeshw.sensors.proximity: yeshw.dPad: nohw.lcd.height: 1920vm.heapSize: 256skin.dynamic: yeshw.device.manufacturer: Googlehw.gps: yeshw.audioInput: yesimage.sysdir.1: system-images\android-28\google_apis_playstore\x86\showDeviceFrame: yeshw.camera.back: virtualsceneAvdId: Pixel_2_API_28hw.lcd.density: 420hw.arc: falsehw.device.hash2: MD5:55acbc835978f326788ed66a5cd4c9a7fastboot.forceChosenSnapshotBoot: nofastboot.forceFastBoot: yeshw.trackBall: nohw.battery: yeshw.sdCard: yestag.display: Google Playruntime.network.latency: nonedisk.dataPartition.size: 6442450944hw.sensors.orientation: yesavd.ini.encoding: UTF-8hw.gpu.enabled: yes

注意:以下示例仅在安卓虚拟设备上运行测试,并没有在真实的设备上运行测试。

项目

效果:

在这里插入图片描述

新建项目,选择 Empty Activity,在配置项目时,Minimum SDK 选择 API 16: Android 4.1 (Jelly Bean)

编辑 src\main\res\layout\activity_main.xml 布局文件,添加一个 Button 组件:

编辑 MainActivity 文件:

package com.mk;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {       @Override    protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        View decorView = getWindow().getDecorView();        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {               @Override            public void onSystemUiVisibilityChange(int visibility) {                   // Note that system bars will only be "visible" if none of the                // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {                       // The system bars are visible. Make any desired                    // adjustments to your UI, such as showing the action bar or                    // other navigational controls.                    Toast.makeText(MainActivity.this, "Visible", Toast.LENGTH_LONG).show();                } else {                       // The system bars are NOT visible. Make any desired                    // adjustments to your UI, such as hiding the action bar or                    // other navigational controls.                    Toast.makeText(MainActivity.this, "Invisible", Toast.LENGTH_LONG).show();                }            }        });        ((Button) findViewById(R.id.buttonImmersive)).setOnClickListener(new View.OnClickListener() {               @Override            public void onClick(View v) {                   immersive();            }        });    }    private void immersive() {           View decorView = getWindow().getDecorView();        decorView.setSystemUiVisibility(                View.SYSTEM_UI_FLAG_IMMERSIVE                // Set the content to appear under the system bars so that the                // content doesn't resize when the system bars hide and show.                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                // Hide the nav bar and status bar                | View.SYSTEM_UI_FLAG_FULLSCREEN                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION        );    }}

参考

上一篇:Android - Broadcasts overview(不完整)
下一篇:Android - Enable fullscreen mode

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年05月07日 02时51分31秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

18 个一线工作中常用 Shell 脚本【实用版】 2023-01-24
element-ui:el-input输入数字-整数和小数 2023-01-24
ElementUI-el-progress改变进度条颜色跟文字样式 2023-01-24
ELK原理与介绍(转) 2023-01-24
ELK学习笔记(三)单台服务器多节点部署 2023-01-24
ELK应用日志收集实战 2023-01-24
elTable火狐浏览器换行 2023-01-24
15个Python数据处理技巧(非常详细)零基础入门到精通,收藏这一篇就够了 2023-01-24
2023年深信服、奇安信、360等大厂网络安全校招面试真题合集(附答案),让你面试轻松无压力! 2023-01-24
2024年全国程序员平均薪资排名:同样是程序员,为什么差这么多?零基础到精通,收藏这篇就够了 2023-01-24
0基础成功转行网络安全工程师,年薪30W+,经验总结都在这(建议收藏) 2023-01-24
100个电脑常用组合键大全(非常详细)零基础入门到精通,收藏这篇就够了 2023-01-24
10个程序员可以接私活的平台 2023-01-24
10个运维拿来就用的 Shell 脚本,用了才知道有多爽,零基础入门到精通,收藏这一篇就够了 2023-01-24
10条sql语句优化的建议 2023-01-24
10款宝藏编程工具!新手必备,大牛强烈推荐! 从零基础到精通,收藏这篇就够了! 2023-01-24
10款最佳免费WiFi黑客工具(附传送门)零基础入门到精通,收藏这一篇就够了 2023-01-24
15个Python数据分析实用技巧(非常详细)零基础入门到精通,收藏这一篇就够了 2023-01-24
15个备受欢迎的嵌入式GUI库,从零基础到精通,收藏这篇就够了! 2023-01-24
15个程序员常逛的宝藏网站!!从零基础到精通,收藏这篇就够了! 2023-01-24