如何在Bottombar上方显示Snackbar?

6
我正在使用 roughike 的 BottomBar 2.0: https://github.com/roughike/BottomBar/ 当我显示 SnackBar 时,它会出现在 BottomBar 上。
我希望它能绘制在 BottomBar 上方。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/here"
        android:background="#fff">

        <FrameLayout
            android:id="@+id/contentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottomBar">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Three Buttons Bar"
                android:id="@+id/textView"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:textColor="@color/colorPrimary"
                android:textSize="35sp" />

        </FrameLayout>

        <com.roughike.bottombar.BottomBar
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            app:bb_tabXmlResource="@xml/bottombar_tabs" />
    </RelativeLayout>

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

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {
                switch (tabId) {
                    case R.id.recent_item:
                        Snackbar.make(findViewById(R.id.main_activity), "Recent Item Selected", Snackbar.LENGTH_LONG).show();
                        break;
                    case R.id.favorite_item:
                        Snackbar.make(findViewById(R.id.main_activity), "Favorite Item Selected", Snackbar.LENGTH_LONG).show();
                        break;
                    case R.id.location_item:
                        Snackbar.make(findViewById(R.id.main_activity), "Location Item Selected", Snackbar.LENGTH_LONG).show();
                        break;
                }
            }
        });
    }
}

截图:

BottomBar Only BottomBar hidden when SnackBar appears

布局文件有问题吗??
我错过了什么??

我也检查了这个:将snackbar移动到底部栏上方,但没有帮助...

编辑:

我尝试了像Abtin说的那样

Snackbar.make(bottomBar, "Location Item Selected", Snackbar.LENGTH_LONG).show();

并且

<com.roughike.bottombar.BottomBar
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            app:bb_tabXmlResource="@xml/bottombar_tabs"
            app:bb_behavior="underNavbar"/>

现在它变成了这样:

Bottom Bar with extra space below That space filled by Snackbar

正如您所见,当我设置app:bb_behavior="underNavbar"时,在BottomBar下方有这个未使用的空间,这不是我想要的。
6个回答

10

新的材料设计中,Snackbar有了新的外观... 你可以使用这行代码在BottomBar上方显示Snackbar:

Snackbar snackbar = Snackbar.make(parentView, text, duration);
snackbar.setAnchorView(bottomBar);

这样做就可以了。


这个答案是正确的。 - M.ghorbani

6

您需要在xml中创建CoordinatorLayout,从其底部显示SnackBar,并将CoordinatorLayout的id作为fun.make SnackBar视图的id。

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/viewSnack"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"/>

活动内部:

Snackbar.make(findViewById(R.id.viewSnack), "Text of the SnackBar", Snackbar.LENGTH_LONG).show();

0

尝试更改传递给Snackbar的视图。

Snackbar.make(findViewById(R.id.contentContainer), "Recent Item Selected", Snackbar.LENGTH_LONG).show();    

0

你是如何使用AHBottomNavigation的呢?我也在用,但Snackbar不会显示在它上方,而是在它前面 :( - hildegard

0

根据协调布局行为的实现方式,您可能需要将底部栏视图本身传递给Snackbar.make()的第一个参数。

Snackbar.make(bottomBar, "Location Item Selected", Snackbar.LENGTH_LONG).show();

另一种可能是您在布局中的底部栏小部件上缺少app:bb_behavior属性。

已编辑问题,请检查。 - Gokul NC

0

非常简单!在你的activity_main.xml文件中为你的根布局声明一个ID:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linerMain">

然后在 Snackbar 的 makeview 字段中,按照以下方式操作:

Snackbar snackbar = Snackbar.make(findViewById(R.id.linerMain),"name",Snackbar.LENGTH_INDEFINITE);

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