Snackbar在约束布局中与浮动操作栏重叠

5
我第一次使用约束布局,想要在活动启动时显示一个 Snackbar。Snackbar 已经显示出来了,但它重叠了我的浮动操作菜单。我尝试了所有的解决方案,但仍然没有解决问题。
这是 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/ListViewCatalog"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:choiceMode="multipleChoice"
    android:clickable="true"
    android:focusable="true">

</android.support.v7.widget.RecyclerView>

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dip"
    android:orientation="horizontal">


    <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/fab_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:paddingBottom="@dimen/_10sdp"
        android:paddingEnd="@dimen/_16sdp"
        android:paddingRight="@dimen/_16sdp"
        fab:menu_fab_label="Pay"
        fab:fab_colorNormal="#DA4336"
        fab:fab_colorPressed="#E75043"
        fab:fab_colorRipple="#99FFFFFF"
        fab:fab_shadowColor="#66000000"
        fab:fab_showShadow="true"
        fab:menu_backgroundColor="#00000000"
        fab:menu_fab_hide_animation="@anim/fab_scale_up"
        fab:menu_fab_show_animation="@anim/fab_scale_down"
        fab:menu_labels_colorNormal="#333333"
        fab:menu_labels_colorPressed="#444444"
        fab:menu_labels_colorRipple="#66FFFFFF"
        fab:menu_labels_ellipsize="end"
        fab:menu_labels_maxLines="-1"
        fab:menu_labels_position="left"
        fab:menu_labels_showShadow="true"
        fab:menu_labels_singleLine="true"
        fab:menu_openDirection="up"
        tools:ignore="RtlSymmetry">

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/add"
            fab:fab_label="Process"
            fab:fab_size="mini" />

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/fab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/refresh"
            fab:fab_label="Reload"
            fab:fab_size="mini" />

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/sub"
            fab:fab_label="Next"
            fab:fab_size="mini" />

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/fab3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/more"
            fab:fab_label="Previous"
            fab:fab_size="mini" />

    </com.github.clans.fab.FloatingActionMenu>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

Java部分:
FloatingActionMenu fam = findViewById(R.id.fab_menu);
    mCartList = CartHelper.getCart();

    // Make sure to clear the selections
    for (int i = 0; i < mCartList.size(); i++) {
        mCartList.get(i).selected = false;
    }

    if (mCartList.size() == 0) {
        Snackbar snackbar = Snackbar.make(fam, "Cart is Empty", Snackbar.LENGTH_LONG);
        snackbar.show();
    }

这里有一张截图,这样你就可以看到我的意思:

enter image description here

1个回答

4

您需要使用协调布局(CoordinatorLayout),请尝试使用此方法。

<android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@mipmap/ic_launcher" />
    </android.support.design.widget.CoordinatorLayout>

Java 代码:

FloatingActionButton fab;
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, (CharSequence) "Hello World..", 0).show();
            }
        });

@Rumit,我知道你的意思,但看看我的代码,我想在活动创建时就显示snackbar,而不是在用户操作之后。此外,我已经在使用协调布局了。 - Kennedy Kambo
@Thekamble...好的...明白了。 - Rumit Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接