Android 如何在底部应用程序栏上的浮动操作栏(fab)上方显示Snackbar(材料设计)
我正在试用新材料底部应用程序栏。在设计指南中,它被告知“在底部的应用程序条和FAB上插入一个snackbar或toast”,但我不知道该怎么做。有人能帮我吗。以下是我正在使用的代码: main活动Android 如何在底部应用程序栏上的浮动操作栏(fab)上方显示Snackbar(材料设计),android,android-layout,material-design,Android,Android Layout,Material Design,我正在试用新材料底部应用程序栏。在设计指南中,它被告知“在底部的应用程序条和FAB上插入一个snackbar或toast”,但我不知道该怎么做。有人能帮我吗。以下是我正在使用的代码: main活动 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
@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());
}
活动\u main.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" />
问题是,每当我单击fab
按钮时,snackbar就会出现在底部应用程序栏的上方,并将fab推到上方。下面是问题的图片
content\u main
版面中,以便您可以通过findViewById
这将使您的snackbar显示在底部appbar的上方。这是一个已修复的问题。有一个新的API
setAnchorView
,我想,我们将进行下一次更新;有关详细信息,请访问:请使用最新的材料库:
编辑build.gradle(模块:app)并更新材料:1.0.0至1.1.0-alpha04或最新版本:
implementation 'com.google.android.material:material:1.1.0-alpha04'
请使用setAnchorView()方法:-
Snackbar.make(CoordinatorLayoutView, "your text", Snackbar.LENGTH_SHORT)
.setAnchorView(BottomAppBarView or FabButtonView).show();
为了让这个答案完整,检查一下。谢谢你们,我会测试它,同时,你们能帮我解决这个问题吗?请编辑你们的答案并使用代码示例以提高可读性。
Snackbar.make(CoordinatorLayoutView, "your text", Snackbar.LENGTH_SHORT)
.setAnchorView(BottomAppBarView or FabButtonView).show();