首页 > 解决方案 > 在android studio中显示和隐藏栏的稳定视图

问题描述

我正在尝试显示和隐藏视图的状态、导航栏和控制器等。问题是,每当条形图和控制器隐藏和显示时,整个视图都会分别上下移动一点,然后回到原来的位置。无论屏幕上是否出现条形,它看起来都非常令人不安,并且需要保持稳定。这是我的实现

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/activityLayout">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <com.google.android.exoplayer2.ui.PlayerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/player"
        android:background="@color/black"
        app:player_layout_id="@layout/exo_simple_player_view"
        />
</RelativeLayout>

这是隐藏和显示栏的java文件

  public boolean onSingleTapUp(MotionEvent e) {
       ActionBar actionBar = getSupportActionBar();
       if (!actionBar.isShowing()){
            showSystemUi();
            playerView.showController();
}
       else {
             hideSystemUI();
             playerView.hideController();
} 
         return true;
}

   private void showSystemUi() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

    }
    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
                        | View.SYSTEM_UI_FLAG_FULLSCREEN 
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }

任何建议都会有所帮助谢谢。

标签: androidview

解决方案


如果您只想隐藏视图,但保留它占用的空间,只需使用 example findViewById<Toolbar>(R.id.toolbar).setVisibility(View.INVISIBLE)

但是如果你真的需要隐藏系统栏(顺便说一句,系统栏!=工具栏),视图会自动扩展到屏幕的全尺寸。我能想到的一种避免这种扩展的方法是将系统栏插入动态添加到屏幕内容中。您必须在隐藏系统状态栏之前计算插图,然后将插图添加到工具栏。如果您随后再次使系统栏可见,则需要再次删除插入。


推荐阅读