【问题标题】:How to show Snackbar above Bottombar?如何在底部栏上方显示 Snackbar?
【发布时间】:2017-02-26 19:19:20
【问题描述】:

我使用的是 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;
                }
            }
        });
    }
}

截图:

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

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

编辑:

我试过as Abtin said

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"/>

现在变成了这样:

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

【问题讨论】:

    标签: android material-design android-snackbar android-navigation-bar


    【解决方案1】:

    有了新的材料设计,Snackbars 有了新的外观.. 你可以用这条线在 BottomBar 上方显示snackbar:

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

    这样就可以了。

    【讨论】:

    • 这个答案是正确的
    【解决方案2】:

    你应该在 xml 中创建 CoordinatorLayout,从他的底部你希望将显示 SnackBar 并将 CoordinatorLayout 的 id 作为 View 的 id 在 fun。制作 SnackBar。

        <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();
    

    【讨论】:

      【解决方案3】:

      尝试更改您传递给小吃店的视图。

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

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

        • 编辑了问题.. 请检查
        【解决方案5】:

        我从Roughike's BottomBar 2.0 切换到Roughike's BottomBar 1.4..

        现在它按预期工作(当然代码更改)..

        我也试过AHBottomNavigation,它很酷,也能按预期工作..

        我没有将此标记为答案,因为它没有回答问题;)

        【讨论】:

        • 你是如何让它与 AHBottomNavigation 一起工作的?我也在用它,但是snackbar没有显示在它上面,而是在它前面:(
        【解决方案6】:

        这很简单! 在您的 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);
        

        【讨论】:

          猜你喜欢
          • 2019-03-06
          • 2017-08-14
          • 2020-06-26
          • 2016-11-03
          • 2016-01-31
          • 2020-07-01
          • 2022-09-22
          • 2019-08-03
          • 1970-01-01
          相关资源
          最近更新 更多