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

本文共 5687 字,大约阅读时间需要 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

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月21日 00时11分51秒