【问题标题】:How can you adjust Android SnackBar to a specific position on screen如何将 Android SnackBar 调整到屏幕上的特定位置
【发布时间】:2015-10-08 03:36:38
【问题描述】:

按照新的 android 小吃栏,我正在尝试将其设置为定位在特定的 y 坐标上。这似乎是不可能的。

我已尝试获取快餐栏视图的父级,但是,父级无需对其进行任何操作来设置它的位置。

mSnackBar.getView().getParent();

在深入实际类时,有一个 mView 的实例,而 mParent 是私有的,无论如何都无法访问。

https://developer.android.com/reference/android/support/design/widget/Snackbar.html

【问题讨论】:

    标签: android android-view android-snackbar snackbar


    【解决方案1】:

    可以通过在现有 Activity 布局中定位 android.support.design.widget.CoordinatorLayout 来设置 Snackbar 的显示位置。

    例如,假设您现有的布局是RelativeLayout,您可以添加CoordinatorLayout,如下所示:

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/myCoordinatorLayout"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
    </android.support.design.widget.CoordinatorLayout>
    

    然后,确保将CoordinatorLayout 作为Snackbar.make() 命令的第一个参数传递。

    final View viewPos = findViewById(R.id.myCoordinatorLayout);    
    Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
                                .setAction(R.string.snackbar_action_undo, showListener)
                                .show();
    

    这将导致 Snackbar 显示在提供给 make() 函数的CoordinatorLayout底部

    如果您传递的 View 不是 CoordinatorLayout,则 Snackbar 将向上遍历树,直到找到 CoordinatorLayout 或布局的根。

    【讨论】:

    • @BrentM,有什么方法可以根据设计指南进行显示 - 即居中+浮动,底部有一些填充?我把它左对齐而不用填充。 google.com/design/spec/components/snackbars-toasts.html
    • @Angel 添加带有android:paddingBottom="16dp"android:layout_width="match_parent" 的CoordinatorLayout 应该允许您控制定位。
    • 这似乎很完美。我的要求是在一个片段中显示 2 个小吃店。因此,我添加了 2 个协调器布局并将它们传递给各自的 Snackbar 对象。然而,当第二个出现时,第一个消失了。有没有办法让它们都可见?
    • 如果您使用的是 Android X,您可以使用 setAnchorView 方法。 developer.android.com/reference/com/google/android/material/…
    【解决方案2】:

    为 Snackbar 明确定义布局可能并不总是可行的。

    问题似乎是针对父母,而不是 Snackbar。

    Snackbar snackbar = Snackbar.make(layout, message, duration);
    View snackbarLayout = snackbar.getView();
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT
    );
    // Layout must match parent layout type
    lp.setMargins(50, 0, 0, 0);
    // Margins relative to the parent view.
    // This would be 50 from the top left.
    snackbarLayout.setLayoutParams(lp);
    snackbar.show();
    

    【讨论】:

    • 我根据你的回答做了一个仓库:github.com/LucasFebatis/customsnackbar
    • @LucasBatista 如果您打算在此处做广告,最好在此处提供署名。
    • 对不起,我不明白。我如何提供归属?
    • @LucasBatista 通常它是通过提供答案链接的评论完成的
    【解决方案3】:

    除了brentm的回答,我不得不使用:

    androidx.constraintlayout.widget.ConstraintLayout

    并为其添加依赖项:

    implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"

    【讨论】:

      【解决方案4】:

      我有一个有趣的想法要告诉你。

      您只需使用下面的代码即可到达您想要的位置。

      Snackbar.make(View,Message , Snackbar.LENGTH_LONG)
      .setAnchorView(pb)
      .show();
      

      这里setAnchorView是用来改变位置的。您可以在 xml 文件中创建布局。只需在setAnchorView 中调用您的布局,它就会显示在 xml 布局的位置上。

      【讨论】:

        猜你喜欢
        • 2020-03-29
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        • 1970-01-01
        • 2011-02-03
        • 2021-04-06
        • 2015-08-10
        • 1970-01-01
        相关资源
        最近更新 更多