android - snackbar 未出现在 viewpager 内的 fragment 布局/回收器 View 顶部

标签 android android-snackbar

我试图让 snackbar 低于 3 个选项卡。我已经膨胀了一个 fragment 并在其中实现了协调器布局。我无法在选项卡下方的顶部显示 snackbar 。并且 getView() 在 float 按钮上方的底部显示 snackbar ,这代码How to show Snackbar at top of the screen在顶部显示我不想要的 snackbar 。如何使 snackbar 显示在 fragment 中的选项卡下方?

代码:

 Snackbar snackbar = Snackbar
                        .make(coordinatorLayout_snackbar, “Snackbar”, Snackbar.LENGTH_INDEFINITE)
                        .setAction(“click”, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Log.e(TAG, "button clicked");
                            }
                        });

                snackbar.show();

xml:

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout_snackbar”
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
</android.support.design.widget.CoordinatorLayout>


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>

最佳答案

您需要将 SwipeRefreshLayout 包装在 CoordinatorLayout 内,如下所示:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout_snackbar”
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

</android.support.design.widget.CoordinatorLayout>

在当前布局中,SwipeRefreshLayout 绘制在 CoordinatorLayout 之上,CoordinatorLayout 的高度为 0,因为它没有子项,并且高度设置为wrap_content。

关于android - snackbar 未出现在 viewpager 内的 fragment 布局/回收器 View 顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34977107/

相关文章:

android - 当我回到上一个 Activity 时 SnackBar 仍然显示

Android 手机作为 USB 键盘/HID-Gadget/

Android - 尝试添加图像时会创建一个空白图像

Android:如何强制 TextToSpeech 说出句号 "."

android - 将 snackbar 中 ImageView 的宽度与 Android 中的自定义 View 的父级匹配不起作用

android - 带有 TranslucentNavigation 的 Snackbar 和 CollapsingToolbarLayout

android - Snackbar 在替换另一个时有时不显示

Android - Sencha Touch 2 PhoneGap 4.0.x 问题

Android,finish()关闭应用程序而不是 Activity

android-snackbar - 如何在 AndroidX 中使用 Snackbar?