android - 防止布局在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>
解决方案
为防止视图移动/布局调整大小,支持状态栏和导航 - 获取活动的当前窗口并在onCreate()
.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
并且无需更改onWindowFocusChanged
或xml
归档,您可以检查程序。我认为您应该使用另一个boolean
inif..else
而onSingleTapUp(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;
}
推荐阅读
- asp.net-mvc - 如何使用 chart.js 根据用户输入创建图表
- node.js - 节点评估脚本没有按预期工作
- python-3.x - 如何解决赋值前引用的局部变量并正确返回模板中的表单数据?
- rest - rest api 嵌套结构怎么表达?
- angular - Angular:div ngIf* 中的条件自定义管道
- python - 船舶到海岸线的距离(以海里为单位)
- apache-flink - 如果我的 Flink 应用程序需要高并行接收器,最佳实践是什么?
- selenium - Selenium 不支持的命令扩展问题
- javascript - d3液态水图从d3v3到v4的转换
- java - 如何在我的 Spring MVC 应用程序 war 文件之外配置 log4j.xml 文件