我试图让 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/