如何在放置在底部应用栏上的FloatingActionBar(fab)上方显示Snackbar(材质设计)

时间:2018-10-01 06:09:21

标签: android android-layout

我正在尝试新的底部应用栏。在设计指南中,它被告知“在底部的应用程序栏和FAB上方插入小吃店或吐司”,但我不确定该怎么做。谁能帮我。以下是我正在使用的代码:

MainActivity

public SearchSourceBuilder createQueryForCount(QueryBuilder queryBuilder, int start, boolean fetchSource, String field){
        logger.info("Creating aggregation count ");

        QueryBuilder finalQuery = QueryBuilders.boolQuery().must(queryBuilder);

        AggregationBuilder aggregationCount = AggregationBuilders.terms("agg").field(USER_ID)
                .subAggregation(AggregationBuilders.topHits("top").explain(false).from(start))
                .subAggregation(AggregationBuilders.count("count").field(field));

        return new SearchSourceBuilder()
                .query(finalQuery)
                .fetchSource(fetchSource)
                .from(start)
                .aggregation(aggregationCount);

    }

activity_main.xml

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show());
}

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorAccent"
        app:fabAlignmentMode="end" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/bar"
        app:layout_anchorGravity="bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

渐变

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />

问题在于,每当我单击android { compileSdkVersion 28 defaultConfig { applicationId "..." minSdkVersion 22 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' } buildToolsVersion = '28.0.2' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.0-alpha4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4' } 按钮时,小吃栏就会出现在底部应用程序栏的上方,并且还会将fab推到上方。下面是问题的图像。

material design snackbar plroblem

4 个答案:

答案 0 :(得分:1)

  1. 在您的content_main布局中添加一个布局ID,以便您可以通过findViewById在活动代码中获取对该ID的引用
  2. 代替传递fab视图,而使用Snackbar.make( view
  3. 中content_main的布局视图

这应该使您的快餐栏出现在BottomAppBar上方。

答案 1 :(得分:1)

这是一个已修复的issue。我认为有一个新的API setAnchorView,我们将继续进行下一个更新;有关详细信息,请访问:https://github.com/material-components/material-components-android/commit/868e80ed6ff183cceeb9c33523bf47f72772c52a

答案 2 :(得分:0)

请使用最新的材料库:

编辑build.gradle(模块:应用程序)并将材料:1.0.0更新为1.1.0-alpha04或最新版本:

    implementation 'com.google.android.material:material:1.1.0-alpha04'

答案 3 :(得分:0)

请使用setAnchorView()方法:-

Snackbar.make(CoordinatorLayoutView, "your text", Snackbar.LENGTH_SHORT)
                        .setAnchorView(yourfabView.getVisibility() == View.VISIBLE ? YourFabView : BottomAppBarView).show();