android中的导航和状态栏

问题描述

我试图隐藏和显示控制器、导航和点击状态。问题是我无法隐藏底部的空白区域。另一件事是我不知道如何在隐藏时显示导航和状态栏。

if (!actionBar.isShowing()) {
  actionBar.show();
  playerView.showController();

 } else {

     getwindow().getDecorView().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_IMMERSIVE_STICKY
     | View.SYstem_UI_FLAG_HIDE_NAVIGATION
     | View.SYstem_UI_FLAG_FULLSCREEN);
     actionBar.hide();
     playerView.hideController();
 }

这是布局文件

这是布局文件

<?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:animateLayoutChanges="true"
    android:fitsSystemWindows="true"
    android:id="@+id/activity_player_layout"
    >
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:elevation="4dp"
        app:titleTextColor="@color/white"
        android:layout_height="?attr/actionBarSize"
        />

   <com.google.android.exoplayer2.ui.PlayerView
      android:id="@+id/exoplayer_video"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/black"
      app:bar_height="4dp"
      app:controller_layout_id="@layout/custom_controller"
      app:player_layout_id="@layout/exo_simple_player_view"
      app:resize_mode="fit"
      app:shutter_background_color="@color/black"
      app:use_controller="true"
      app:use_sensor_rotation="true">

      </com.google.android.exoplayer2.ui.PlayerView>

</RelativeLayout>

解决方法

您需要为主布局设置点击侦听器,但使用相对布局作为主布局,因为我认为相对布局会起作用

layout.setOnClickListener(new View.OnClickListener() {

    boolean show = true;

    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"CLICK",Toast.LENGTH_SHORT).show();
        if (show) {
              toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
            hideSystemUI();
            show = false;
        } else {
            toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
              showSystemUI();
              show = true;
     }
 }
});


private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    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 // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);

}

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);


}