2

我有这个布局

<?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轴)

4

7 回答 7

3

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

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

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

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

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

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

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

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

于 2020-03-27T10:23:59.960 回答
0

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>
于 2020-04-03T09:01:43.733 回答
0

Snackbar 在不同的场景中实现有很多限制,你可以将这个库用于相同的SuperToasts

SuperActivityToast.create(context, new Style(), Style.TYPE_BUTTON).setButtonText("Open").setAnimations(Style.ANIMATIONS_POP).show();
于 2020-03-27T20:37:19.480 回答
0

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

于 2020-03-28T19:25:55.300 回答
0

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

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

于 2020-03-21T19:52:09.370 回答
0

我认为这会奏效。

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);
于 2020-03-21T19:58:07.660 回答
0

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

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

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

于 2020-03-26T21:47:40.307 回答