Android - 全屏而不移动内容

标签 android android-toolbar android-statusbar android-fullscreen android-window

我正在开发一个阅读应用程序,用户可以从中阅读多种类型的文档。
此应用程序还具有通过触摸屏幕的任何部分启用全屏的功能。我可以通过使用以下代码行隐藏状态栏和工具栏 (ActionBar) 来启用/禁用全屏:

if (toolbar.getVisibility() == View.VISIBLE) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    appbar.animate().translationY(-112).setDuration(600L)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    toolbar.setVisibility(View.GONE);
                }
            }).start();
} else {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    toolbar.setVisibility(View.VISIBLE);
    appbar.animate().translationY(0).setDuration(600L).start();
}  
但真正的问题是,当我启用全屏时,内容会向上移动,而禁用时会向下移动。
我不想移动我的内容。我在 Google doc 应用程序中看到启用/禁用全屏时内容不会移动。
使用 RelativeLayout而不是 CoordinatorLayout作为根布局,我可以避免工具栏推送内容,但是当我隐藏状态栏时它会推送一点。
请帮忙。

最佳答案

Using RelativeLayout instead of CoordinatorLayout as a root layout I can avoid toolbar pushes the content but it pushes a little bit when I hide the status bar.


现在您的问题是状态栏在显示 Activity 内容时会向下推。
因此,要解决此问题,您可以通过将以下内容添加到 Activity 的 onCreate() 来使状态栏与 Activity 窗口重叠。方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
                         WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
更新

what to use if build SDK is lower than kitkat?


要支持 Kitkat 以下的 API,请尝试为 (v-19) 限定符创建一个 style.xml 文件,并添加以下属性
<item name="android:windowTranslucentStatus">true</item> 
<item name="android:windowTranslucentNavigation">true</item>
更新:使用自定义 ActionBar
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:text="@string/long_text"
                android:textColor="@android:color/white" />

        </ScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:background="#9E0557B5"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
行为
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
        setSupportActionBar(toolbar);

        TextView content = findViewById(R.id.content);
        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getSupportActionBar().isShowing()) {
                    getSupportActionBar().hide();
                    showSystemBar(false);
                }
                else {
                    getSupportActionBar().show();
                    showSystemBar(true);
                }
            }
        });
    }

    private void showSystemBar(boolean isDisplayed) {

        int uiOptions;
        if (isDisplayed) {
            // show status bar
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            // hide status bar
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
                getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            } else {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        }

    }

}
使用NoActionBar主题 int style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
覆盖 style.xml (v.19)
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>
预习

关于Android - 全屏而不移动内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62825134/

相关文章:

android - 从振幅计算分贝 - Android 媒体记录器

android - 工具栏和菜单上的阴影未显示在重复布局中

android - 如何使 colorPrimary 不显示为工具栏背景

android - 如何隐藏状态栏?

android - 奥利奥通知栏圆形图标

android - Android 中的 getSize() 是否包括状态栏的高度?

android - 无法为 BuildType_Decorated 获取 'applicationVariants' 的未知属性

android - EditText 始终显示带 2 位小数的数字

java - 如何以编程方式查看 EditText 是否具有日期输入类型?

java - 删除工具栏副标题