如何将插图应用于内部具有图像的CollapsingToolbarLayout,以便工具栏中的图像绘制在状态栏后面?

我正在尝试使用插图为Android 10设置全屏活动。我想要在状态栏后面绘制工具栏中的图像。我尝试以不同的组合使用java.sql.*标志,但是它不起作用,android:fitsSystemWindows没有正确的填充,并且状态栏与工具栏菜单控件略有重叠。因此,我使用了方便的AppBarLayout包装器Insetter library by Chris Banes根据系统窗口插图来设置填充。

这是我的布局:

WindowInsetsCompat

这是我在代码中设置填充的方法:

<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:id="@+id/book_activity_root"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:animateLayoutChanges="true"
  tools:context="com.bookcrossing.mobile.ui.bookpreview.Bookactivity"
  >

  <androidx.core.widget.nestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >
    ...
  </androidx.core.widget.nestedScrollView>
  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/toolbarContainer"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    >
    <com.google.android.material.appbar.CollapsingToolbarLayout
      android:id="@+id/collapsingToolbarContainer"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:contentScrim="?attr/colorPrimary"
      app:expandedTitleGravity="bottom"
      app:layout_scrollflags="scroll|exitUntilCollapsed|snap"
      app:titleEnabled="true"
      app:toolbarId="@id/toolbar"
      >

      <ImageView
        android:id="@+id/cover"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/cover_description"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        app:layout_collapseMode="parallax"
        app:layout_collapseParallaxMultiplier="0.5"
        />

      <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:layout_scrollflags="scroll|enterAlways"
        tools:title="War and Peace"
        />

    </com.google.android.material.appbar.CollapsingToolbarLayout>
  </com.google.android.material.appbar.AppBarLayout>
  <com.google.android.material.floatingactionbutton.FloatingactionButton
    android:id="@+id/favorite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/default_padding"
    android:clickable="true"
    android:focusable="true"
    android:src="@drawable/ic_turned_in_not_white_24dp"
    app:layout_anchor="@id/toolbar_container"
    app:layout_anchorGravity="bottom|right|end"
    />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我为全屏模式设置了窗口标志:

    Insetter.setOnApplyInsetsListener(toolbarContainer,(view,windowInsets,initial) -> {
      view.setPadding(initial.getPaddings().getLeft(),windowInsets.getSystemWindowInsetTop() + initial.getPaddings().getTop(),initial.getPaddings().getRight(),initial.getPaddings().getBottom());
    });

    Insetter.setOnApplyInsetsListener(cover,initial) -> {
      ViewGroup.MarginlayoutParams params = (ViewGroup.MarginlayoutParams) view.getLayoutParams();
      params.topMargin = windowInsets.getSystemWindowInsetTop() + initial.getMargins().getTop();
      view.setLayoutParams(params);
    });

    Insetter.setOnApplyInsetsListener(nestedScrollView,initial.getPaddings().getTop(),windowInsets.getSystemWindowInsetBottom() + initial.getPaddings().getBottom());
    });

    Insetter.setOnApplyInsetsListener(favorite,initialPadding) -> {
      view.setPadding(initialPadding.getPaddings().getLeft(),initialPadding.getPaddings().getTop(),windowInsets.getSystemWindowInsetRight() + initialPadding.getPaddings().getRight(),initialPadding.getPaddings().getBottom());
    });

这是图片中的结果:

如何将插图应用于内部具有图像的CollapsingToolbarLayout,以便工具栏中的图像绘制在状态栏后面?

状态栏设置为透明,蓝色来自具有顶部填充的工具栏。

最后,我希望在状态栏后面绘制图像,这有可能吗?

我正在Android 10模拟器上进行测试。

ttdrhfiaid 回答:如何将插图应用于内部具有图像的CollapsingToolbarLayout,以便工具栏中的图像绘制在状态栏后面?

您的标志似乎是错误的。您应为导航栏设置标志,而应为状态栏设置标志。

这是我使用的代码(科特琳):

requireActivity().window?.decorView?.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)

使UI在状态栏后面隐藏,而不会隐藏状态栏。然后,在不应该与状态栏(例如按钮和其他控件)重叠的视图上使用此扩展功能:

fun View.alignBelowStatusBar() {
this.setOnApplyWindowInsetsListener { view,insets ->
    val params = view.layoutParams as ViewGroup.MarginLayoutParams
    params.topMargin = insets.systemWindowInsetTop
    view.layoutParams = params
    insets
}

}

因此,第一个标志将确保您的背景/图像进入状态栏。第二个是确保状态栏不会覆盖您的控件。

,

我记得您不需要在CoordinatorLayout中手动支持填充,请参见pos类中的while (Vector2.Distance(transform.position,playerPos) > hookshotDistanceBuffer) { transform.position = Vector2.MoveTowards(transform.position,playerPos,hookshotReturnSpeed * Time.deltaTime); 函数。要支持在状态栏后面绘制图形,应将setupForInsets设置为CoordinatorLayout

android:fitsSystemWindows="true"

据我了解,您在这里不需要CoordinatorLayout。它负责布局更改中的动画,例如删除/添加和显示/隐藏视图。

本文链接:https://www.f2er.com/3155701.html

大家都在问