阻止在Android中调整布局大小

2022-08-22 00:00:00 android show-hide

我正在使用Playerview并尝试显示/隐藏系统栏,但当它们隐藏或显示时,整个屏幕会上下移动。我理解这是因为每次隐藏/显示条时,屏幕都会调整大小。我希望布局停止调整大小,这样屏幕就不会移动。以下代码在横向模式下运行良好,但仅在纵向模式下出现问题。

private boolean hasfocused;

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

        setContentView(R.layout.activity_player_layout);

                         //


 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
          getSupportActionBar().show();
          playerView.showController();

    } else {
        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);
        getSupportActionBar().hide();
        playerView.hideController();
        
    }

@Override
public boolean onSingleTapUp(MotionEvent e) {

         if (hasfocused) {
            onWindowFocusChanged(true);
            hasfocused = false;
        } else {
            onWindowFocusChanged(false);
            hasfocused = true;
        }
   }

且XML为

<?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:id="@+id/activity_player_layout"
    >
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:elevation="4dp"
        android:fitsSystemWindows="true"
        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"
      android:fitsSystemWindows="true"
      app:controller_layout_id="@layout/custom_controller"
      app:player_layout_id="@layout/exo_simple_player_view"
      >

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

</RelativeLayout>

解决方案

为防止视图移动/布局调整大小,支持状态栏和导航-获取活动的当前窗口,并在onCreate()中设置下方标志。

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
并且在不更改onWindowFocusChangedxml文件的情况下,您可以检查程序。我认为您应该在onSingleTapUp(MotionEvent e)if..else中使用另一个boolean,而不是使用myToolbar.isShown()。这是在焦点获得或失去时制造冲突。

因此在您的活动中使用布尔值private boolean hasfocused;

然后更改onSingleTapUp如下-

   @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (hasfocused) {
                onWindowFocusChanged(true);
                hasfocused = false;
            } else {
                onWindowFocusChanged(false);
                hasfocused = true;
            }

            return true;
        }

如果需要触发上述方法,则onDownonSingleTapConfirmed也返回TRUE。

@Override
        public boolean onDown(MotionEvent event) {
            return true;
        }

 @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return true;
        }



    

相关文章