【问题标题】:How would can I make the snackbar to be above the navigation bar? (y axis)我怎样才能使小吃栏位于导航栏上方? (y轴)
【发布时间】:2020-03-18 16:13:16
【问题描述】:

我有这个布局

<?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小吃店。

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

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

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

【问题讨论】:

  • 能否请您提供所需的代码模板,以便从我这边重现问题。
  • @Elad Benda 尝试使用 CoordinatorLayout 作为 root。

标签: android android-snackbar android-navigation-bar


【解决方案1】:

您可能拥有您的layout fit to system windows。尽管我在您的示例中没有看到它,但这正是它的作用。也许它也可以设置在其他地方。

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

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

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

有一个简单的解决方法。你可以apply window insets

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

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

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

【讨论】:

    【解决方案2】:

    我认为这会起作用。

    Snackbar  snb = Snackbar.make(findViewById(targetView), snackMessage, duration);
    View view = snb.getView();
    view.setBackgroundColor(backgroundColour);
    snb.setActionTextColor(textColor);
    FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity =  Gravity.CENTER_HORIZONTAL | Gravity.TOP;
    params.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
    view.setLayoutParams(params);
    

    【讨论】:

      【解决方案3】:

      我认为在您的Snackbar 中设置elevation 应该可以完成这项工作。

      如果您正在为您的小吃店使用自定义布局,您可以考虑将android:elevation 放在其中。您也可以以编程方式执行此操作。

      【讨论】:

      • 您将 elevation 添加到了 constraintLayout (?) 而不是没有 xml 存在的小吃店?
      • 我认为这是您的小吃店的自定义布局。
      • 我已从我的答案中删除了代码。但是,我认为设置海拔应该适用于您的情况。如果您可以发布一些有关如何显示快餐栏的代码,那就太好了,以便我们看一看。
      【解决方案4】:

      最简单的方法是将 AnchorView 设置为底部导航抽屉(如果可用)。

      Snackbar snackbar= Snackbar.make(view, text, duration);
      snackbar.setAnchorView(bottomBar);
      

      另外请检查您的主题和样式实现。您的问题也可能由于不正确的主题和样式而出现。

      【讨论】:

        【解决方案5】:

        Snackbar在不同的场景下实现有很多限制,你可以用这个库来同一个SuperToasts

        SuperActivityToast.create(context, new Style(), Style.TYPE_BUTTON).setButtonText("Open").setAnimations(Style.ANIMATIONS_POP).show();
        

        【讨论】:

          【解决方案6】:

          我同意@Knossos,您还可以做一件事,您可以获得导航栏的高度,并可以为您的自定义小吃店留出边距或在导航面板上方的视图中显示小吃店

          【讨论】:

            【解决方案7】:

            用于 Snackbar 视图的 ViewGroup.MarginLayoutParams 自 material-1.1.0 库版本以来已停止工作。 要定义自定义边距,您需要在您的styles.xml 中定义自定义Snackbar 的样式:

            <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
            //your items
                <item name="snackbarStyle">@style/Snackbar</item>   //custom snackbar style
            </style>
            
            <style name="Snackbar" parent="Widget.MaterialComponents.Snackbar">
                <item name="android:layout_margin">@null</item>
                <item name="android:layout_marginTop">0dp</item>
                <item name="android:layout_marginLeft">0dp</item>
                <item name="android:layout_marginRight">0dp</item>
                <item name="android:layout_marginBottom">50dp</item>   //custom bottom margin
            </style>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-02-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-24
              • 2023-01-17
              • 2021-04-03
              • 1970-01-01
              相关资源
              最近更新 更多