这是我当前的设置:
我有一个在根布局(CoordinatorLayout)上注册小吃栏的 BaseActivity:
<?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"
android:id="@+id/snackbar_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_dodgeInsetEdges="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/progressbar_with_text" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
现在我添加一个如下所示的片段:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/content_save"
android:tint="@android:color/white"
android:visibility="gone"
app:backgroundTint="@color/secondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:tint="@android:color/white"
app:useCompatPadding="true"
tools:ignore="SpeakableTextPresentCheck"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
我希望晶圆厂在显示时被小吃栏推起,目前它与小吃栏重叠。
我知道使用 CoordinatorLayout 作为根并将 FloatingActionButton (FAB) 放置在其中可以与属性
app:layout_dodgeInsetEdges="bottom"
配合使用。此设置可确保在显示 Snackbar 时将 FAB 向上推。但是,对于子片段,我无法获得类似的结果。是的,我的设置必须保持原样,因为我有多个彼此不同的片段,并且包含独特样式的 FAB,因此将 FAB 移动到活动级别不是一个选项。
您应该在 BaseActivity 中创建一个方法来显示小吃栏,在该方法中通过其 id 找到 coordinatorLayout。
public void showSnackbar(String message, int duration) {
CoordinatorLayout coordinatorLayout = findViewById(R.id.snackbar_container);
Snackbar.make(coordinatorLayout, message, duration).show();
}
你的fragment应该能够访问BaseActivity。确保你的activity实现了某个接口,这将允许fragment与activity进行通信。
public class MyFragment extends Fragment {
private BaseActivity activity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof BaseActivity) {
activity = (BaseActivity) context;
}
}
private void showSnackbar(String message) {
activity.showSnackbar(message, Snackbar.LENGTH_SHORT);
}
}