android - 在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);
}
任何建议都会有所帮助谢谢。
解决方案
如果您只想隐藏视图,但保留它占用的空间,只需使用 example findViewById<Toolbar>(R.id.toolbar).setVisibility(View.INVISIBLE)
。
但是如果你真的需要隐藏系统栏(顺便说一句,系统栏!=工具栏),视图会自动扩展到屏幕的全尺寸。我能想到的一种避免这种扩展的方法是将系统栏插入动态添加到屏幕内容中。您必须在隐藏系统状态栏之前计算插图,然后将插图添加到工具栏。如果您随后再次使系统栏可见,则需要再次删除插入。
推荐阅读
- apache - 如果域配置了另一个 apache 端口到 xampp,HTTPS 不起作用
- python - Tensorflow CIELAB 颜色空间边界
- mysql - MySQL 在 NOT IN 条件下插入和连接表
- java - 将针对 Heroku 开发的 Web 应用部署到 Azure
- css - 使用大众单位时 CSS calc 值不正确
- paypal - PayPal 结帐 - 对于更新的 PayPal 结帐流程,我们需要 IPN 吗?
- apache - 将域重定向到其他域子目录但在地址栏中显示第一个域?
- go - 使用 kubernetes 客户端 API 创建 pod 时出错
- python - 如何使变量名依赖于python中的输入?
- amazon-sqs - 触发 SQS 的 EventBridge 的 SAM 模板