android ?attr得到状态栏高度,android - 状态栏下方的CoordinatorLayout + CollapsingToolbarLayout滚动 - 堆栈内存溢出...

我的布局有一个奇怪的问题。 我有一个带有CollapsingToolBarLayout的CoordinatorLayout 。 我有一个透明的状态栏,可以在顶部更好地显示图像。 因此,我在此ImageView上设置了fitsSystemWindow 。 我现在遇到的问题是,当工具栏完全合拢时,工具栏的顶部滚动到系统状态栏的后面。 有没有办法防止这种行为?

这是屏幕截图:

762ff9bc47ed1eee412fa80d56d29ec0.png

410f523d705d5f204eaf4eefde64f678.png

50de3562b991c71f971c608d29bbaa7b.png

这是我的布局:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/coordinatorLayout">

android:id="@+id/main_appbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:id="@+id/main_collapsing"

android:layout_width="match_parent"

android:layout_height="350dp"

app:layout_scrollFlags="scroll|exitUntilCollapsed"

app:contentScrim="?attr/colorPrimary"

app:expandedTitleMarginStart="140dp"

app:expandedTitleMarginBottom="90dp"

app:expandedTitleMarginEnd="64dp"

android:fitsSystemWindows="true">

app:layout_collapseMode="parallax"

android:id="@+id/icon"

android:transitionName="pic"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

android:src="@drawable/missing_album_art"

android:tint="#AA000000"

android:fitsSystemWindows="true"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginBottom="20dp"

app:layout_collapseMode="parallax">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:padding="20dp"

android:orientation="horizontal">

android:layout_width="100dp"

android:layout_height="100dp"

android:id="@+id/smallicon"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:layout_gravity="bottom"

android:padding="20dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/artist"

android:ellipsize="end"

android:singleLine="true"

android:textSize="24sp"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ellipsize="end"

android:singleLine="true"

android:id="@+id/date"/>

android:id="@+id/main_toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

app:layout_collapseMode="pin" />

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

android:id="@+id/list"

android:dividerHeight="2dp"

android:transitionName="bottom">

另外,在创建活动时,我会稍微调整布局参数:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

好的,我知道你的问题了。针对你的问题,可以通过如下代码实现AppBarLayout滚动监听: ```java AppBarLayout appBarLayout = findViewById(R.id.appBarLayout); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { // 在这里处理滚动事件 } }); ``` 其中,`addOnOffsetChangedListener` 方法可以添加滚动监听器,该监听器中的 `onOffsetChanged` 方法中可以处理 AppBarLayout滚动事件。 关于CoordinatorLayout、AppBarLayoutCollapsingToolbarLayout 和 RecyclerView 的结合使用,可以参考以下代码: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsingToolbarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/image" app:layout_collapseMode="parallax" /> <com.google.android.material.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 其中,`CoordinatorLayout` 是一个可以协调多个子视图之间交互的布局容器。`AppBarLayout` 是一个垂直的 LinearLayout,可以包含一个或多个子视图,用于实现应用程序栏。`CollapsingToolbarLayout` 是一个具有可折叠效果的 Toolbar 布局。`RecyclerView` 是一个可以滚动的列表视图。 以上代码实现了一个具有可折叠效果的 Toolbar 和一个可以滚动的列表视图。在 `CollapsingToolbarLayout` 中的 `ImageView` 和 `Toolbar` 都有一个 `layout_collapseMode` 属性,用于指定它们的行为。`ImageView` 的 `layout_collapseMode` 属性设置为 `parallax`,表示在滚动时具有视差效果;`Toolbar` 的 `layout_collapseMode` 属性设置为 `pin`,表示在折叠时固定在顶部。 `RecyclerView` 的 `layout_behavior` 属性设置为 `appbar_scrolling_view_behavior`,表示它是一个可以与 `AppBarLayout` 协同工作的滚动视图。这样,`AppBarLayout` 就可以根据 `RecyclerView` 的滚动情况来调整自身的显示状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值