首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在底部栏上显示Snackbar?

如何在底部栏上显示Snackbar?
EN

Stack Overflow用户
提问于 2016-10-18 01:47:05
回答 6查看 10.1K关注 0票数 5

我使用的是roughike的BottomBar 2.0:https://github.com/roughike/BottomBar/

当我显示SnackBar时,它就会出现在BottomBar本身上。

我希望它绘制在BottomBar之上。

activity_main.xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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;
                }
            }
        });
    }
}

截图:

布局文件有什么问题吗??

我遗漏了什么??

我还检查了这个:Move snackbar above the bottom bar,但它没有帮助..

编辑:

我试过as Abtin said

代码语言:javascript
复制
Snackbar.make(bottomBar, "Location Item Selected", Snackbar.LENGTH_LONG).show();

代码语言:javascript
复制
<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"/>

现在它变成了这样:

正如您所看到的,当我设置app:bb_behavior="underNavbar"时,BottomBar下面有未使用的空间,这不是我想要的。

EN

回答 6

Stack Overflow用户

发布于 2019-09-10 16:23:16

采用新的材料设计,Snackbar有了新的外观。你可以用下面这行代码在BottomBar上面显示snackbar:

代码语言:javascript
复制
Snackbar snackbar = Snackbar.make(parentView, text, duration);
snackbar.setAnchorView(bottomBar);

这样就可以完成工作了。

票数 9
EN

Stack Overflow用户

发布于 2017-03-30 23:37:44

您应该在xml中创建CoordinatorLayout,从他的底部,您希望将显示为SnackBar,并给出CoordinatorLayout的id作为视图的id。SnackBar的制作。

代码语言:javascript
复制
    <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"/>

内部活动:

代码语言:javascript
复制
Snackbar.make(findViewById(R.id.viewSnack), "Text of the SnackBar", Snackbar.LENGTH_LONG).show();
票数 6
EN

Stack Overflow用户

发布于 2016-10-18 02:05:20

尝试更改传递到快餐栏的视图。

代码语言:javascript
复制
Snackbar.make(findViewById(R.id.contentContainer), "Recent Item Selected", Snackbar.LENGTH_LONG).show();    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40092618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档