首页 > 解决方案 > 我怎样才能使小吃栏位于导航栏上方?(y轴)

问题描述

我有这个布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/non_clickable_account_snackbar_constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <android.support.v7.widget.Toolbar
      android:id="@+id/my_snackbar_toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
  <com.google.android.material.button.MaterialButton
      android:id="@+id/my_snackbar_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="@string/show_account_snackbar"
      android:theme="@style/Theme.GoogleMaterial.DayNight.Bridge"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我有一个自定义课程:

public final class MySnackbar<AccountT>
    extends BaseTransientBottomBar<MySnackbar<myT>> {

super(findTopMostFrameLayout(parent), content, new MySnackbarAnimation(content));

调用时"show()",它会显示一个具有自定义布局的 android 小吃店。

在此处输入图像描述

当我显示 amy_snackbar时,它会在底部导航栏(z 轴)下方看到。

1)我怎样才能让它在导航栏(z轴)的顶部?或者这是常见的行为?

2)你如何让它从顶部到底部的导航栏被看到?(y轴)

标签: androidandroid-snackbarandroid-navigation-bar

解决方案


您的布局可能适合系统窗口。尽管我在您的示例中没有看到它,但这正是它的作用。也许它也可以设置在其他地方。

系统窗口是系统正在绘制非交互式(在状态栏的情况下)或交互式(在导航栏的情况下)内容的屏幕部分。

大多数情况下,您的应用不需要在状态栏或导航栏下进行绘制,但如果这样做:您需要确保交互元素(如按钮)不会隐藏在它们下方。这就是 android:fitsSystemWindows="true" 属性给你的默认行为:它设置视图的填充以确保内容不会覆盖系统窗口。

这意味着它也将位于底部的导航栏和顶部的系统栏下方。

有一个简单的解决方法。您可以应用窗口插图

Chris Banes 在那个博客中有一些很好的例子。

snackbarCustomLayout.setOnApplyWindowInsetsListener { view, insets ->
    view.updatePadding(bottom = insets.systemWindowInsetBottom)
    insets
}

请记住,您可能还需要应用左和/或右插图来处理横向模式。


推荐阅读