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

时间:2018-02-08 10:50:22

标签: android xml android-layout android-constraintlayout floating-action-menu

我第一次使用constraint layout工作,我希望在snackbar启动时显示activitysnackbar正在展示,但与floating action menu重叠。我已经尝试了一切,但没有任何工作。

我发布了我的XML和Java,所以你告诉我可能是什么问题。我已经在网上尝试了所有解决方案但没有任何工作。

这是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 个答案:

答案 0 :(得分: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();
            }
        });