Android-如何仅滚动状态栏后面的图像

问题描述 投票:0回答:1

我有一个带有CollapsingToolbar和NestedScrollView的CoordinatorLayout。NestedScrollView的图像向上滚动(确定)。CollapsingToolbar有一个TabLayout(其中包含文本),当我滚动时,它会显示状态栏图标和文本,那不行,我想在进入状态栏之前隐藏TabLayout。有可能吗?

我有这个:

For better understand, I have this:

当我滚动时,我得到了:enter image description here

但是我想要这个:

enter image description here

如何在nestedScrollview内滚动内容,但隐藏Tabbar?

(更新)现在我有:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true">

        <RelativeLayout
            android:id="@+id/top_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#505000FF"
            app:layout_scrollFlags="scroll|enterAlways">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="MY TAB"
                android:textAlignment="center"
                android:textColor="#FFF"
                android:textSize="21sp" />

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

    <androidx.core.widget.NestedScrollView
        android:id="@+id/content_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/tmb" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \n
            TEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \n
            TEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \n"
                android:textColor="#FFF" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

在我的代码中,我使用以下方法调整边距:

    private void applyTheme() {
        getWindow().setStatusBarColor(Color.parseColor("#60000000"));
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.app_bar), (v, insets) -> {
            ((ViewGroup.MarginLayoutParams) topBar.getLayoutParams()).topMargin += insets.getSystemWindowInsetTop();
            ((ViewGroup.MarginLayoutParams) content.getLayoutParams()).topMargin = -((ViewGroup.MarginLayoutParams) topBar.getLayoutParams()).height - insets.getSystemWindowInsetTop();
            return insets;
        });
    }
android
1个回答
0
投票

您可以在主样式中使用以下几个属性

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>

如下

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>


        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>

    </style>
</resources>

UPDATE 1:

现在可以在ImageView上方看到状态栏,

  1. android:fitsSystemWindows="true"中删除AppBarLayout
  2. android:fitsSystemWindows="false"添加到根视图CoordinatorLayout
  3. 将前面提到的属性保留在样式文件中

因此,您的布局将是:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:fitsSystemWindows="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">

        <RelativeLayout
            android:id="@+id/top_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#505000FF"
            app:layout_scrollFlags="scroll|enterAlways">
....

这是测试时的演示

>

UPDATE 2

您的演示中的行为与我所需要的不同,因为“ 标签”可以从状态栏中看到,我需要在 状态栏,与netflix的主要活动相同。

您可以将AppBarLayout.OnOffsetChangedListener添加到AppBarLayout.OnOffsetChangedListener并根据偏移值隐藏/显示AppBarLayout

我在下面的演示中使用了任意值(-10),但是您可以确定另一个小于0的值。

TextView    
© www.soinside.com 2019 - 2024. All rights reserved.