首页 > 解决方案 > 防止布局在android中调整大小

问题描述

我正在使用播放器视图并尝试显示/隐藏系统栏,但是当它们隐藏或显示时,整个屏幕会上下移动。我知道这是因为每次条形隐藏/显示时屏幕都会调整大小。我希望布局停止调整大小,以便屏幕不会移动。以下代码在横向模式下完美运行,但只会在纵向模式下导致问题。

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>

标签: androidshow-hide

解决方案


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

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);

并且无需更改onWindowFocusChangedxml归档,您可以检查程序。我认为您应该使用另一个booleaninif..elseonSingleTapUp(MotionEvent e)不是使用myToolbar.isShown(). 这是在获得或失去焦点时产生冲突。

private boolean hasfocused;所以在你的 Activity 中取一个布尔值。

然后更改onSingleTapUp如下 -

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

            return true;
        }

如果您需要触发上述方法,则onDown返回true 。onSingleTapConfirmed

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

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



    

推荐阅读