在 IT 技术中,将所有布局都包裹在 CoordinatorLayout 中是一种好的实践吗?

24

我正在考虑一种在我的应用程序中实现Android Snackbars的方法。基本上,我希望能够在应用程序的任何地方显示Snackbar。

我发现,当把android.support.design.widget.Snackbar放在android.support.design.widget.CoordinatorLayout中时,它的性能最佳。否则,我将无法滑动它,它会显示在导航抽屉上方并且不能与浮动操作按钮交互。

所以问题是:是否有好的做法可以将所有布局都包装在CoordinatorLayout,在BaseActivity中获取引用,以便它可以从几乎任何地方传递给Snackbar?

这似乎是确保Snackbar和其他布局组件正确运作的可靠方法,但……这意味着需要触及所有布局,并且必须有一个BaseActivity,该Activity由所有其他Activity扩展,并且从任何想要显示Snackbar的Fragment中访问它。

是否有更好的方法?


这就是我对我的项目所做的。 - gmetax
3个回答

7

我已经实现了类似于在应用程序中的任何位置打开Snackbar的功能。

我创建了一个通用方法,只需传递上下文和要显示的字符串消息,无需传递任何视图。请查看我的代码片段。

 public static void showSnackBar(Activity context, String msg) {
   Snackbar.make(context.getWindow().getDecorView().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG).show();
 }

使用此方法,您可以使您的Snackbar始终显示在底部。

这并没有帮助到CoordinatorLayout的情况。完全不是我所问的! - Marcel Bro
@anoniim,你的问题是“有更好的方法吗?”我认为这个答案完整地回答了Rahat的问题。 - JJ86
嗯,有没有更好的方法使用CoordinatorLayout? 没有它,snack bars就无法进行滑动操作,并且与其他布局元素不兼容。 - Marcel Bro
这是我需要的 +1 - Tejas Pandya
请注意,如果不在CoordinatorLayout中,您将失去滑动关闭的功能。 - WindRider

2
这些是您可以选择的选项。在项目中需要时,请使用其中之一。

最佳方式

做这件事的最佳方式是您在问题中已经提到的,添加一个BaseActivity并将所有活动从它扩展。根据CoordinatorLayout官方文档

CoordinatorLayout主要用于以下两种情况:

  1. 作为顶级应用程序装饰或chrome布局
  2. 作为具有与一个或多个子视图的特定交互的容器

因此,CoordinatorLayout主要是为此目的创建的(虽然还有其他原因)。如文档中所述,性能问题最少。

使用FrameLayout

就像Rainmaker已经回答的那样,您可以在布局文件夹中使用一个带有对CoordinatorLayout布局的引用的活动,其中子项将是一个帧布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/activity_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>

然后,您将只使用一个活动,并使用setContentView(R.layout.root_coordinate_layout)。然后,您将把所有其他活动转换为片段,并使用以下方式添加它们:
MyFragment myf = new MyFragment();
FragmentTransaction transaction = getFragmentManager()
    .beginTransaction()
    .add(R.id.your_layout, myf)
    .commit();

编程方式

这是另一种做同样事情的方式。但这个方法更加复杂,需要做很多工作。

在你的所有活动中,不要使用 setContentView(R.id.your_layout),而要使用以下代码:

LayoutInflater inflater = LayoutInflater.from(this);
ConstraintLayout yourLayout = (ConstraintLayout) inflater.inflate(R.layout.your_layout, null, false);
CoordinatorLayout layout = new CoordinatorLayout(this);
// Set its property as you wish ...
layout.addView(mainScreen);
setContentView(layout);

1

是的,你可以将你的布局封装到一个协调布局中,例如:

<android.support.design.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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <Button
            android:id="@+id/btnSimpleSnackbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="Simple Snackbar" />

        <Button
            android:id="@+id/btnActionCallback"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="With Action Callback" />

        <Button
            android:id="@+id/btnCustomSnackbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Custom Color" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接