【问题标题】:Is it a good practice to wrap all layouts in CoordinatorLayout?将所有布局包装在 CoordinatorLayout 中是一种好习惯吗?
【发布时间】:2016-07-30 02:09:45
【问题描述】:

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

我发现,android.support.design.widget.Snackbar 在放入 android.support.design.widget.CoordinatorLayout 时表现最好。否则,我无法将其滑开,它会显示在导航抽屉上并且不与浮动操作按钮交互。

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

这似乎是确保 Snackbar 和其他布局组件正常运行的可靠方法,但是……嗯,这意味着接触所有布局并拥有一个 BaseActivity 由所有其他活动扩展,并且可以从任何 Fragment 访问想要显示一个 Snackbar。

有没有更好的办法?

【问题讨论】:

  • 这就是我对我的项目所做的。

标签: android android-coordinatorlayout android-snackbar coordinator-layout snackbar


【解决方案1】:

这些是您拥有的选项。在项目中根据需要使用其中之一。

最好的方法

这样做的最佳方法是您在问题中已经说过,添加BaseActivity 并从中扩展您的所有活动。根据CoordinatorLayoutofficial documentation

CoordinatorLayout 适用于两个主要用例:

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

所以CoordinatorLayout 的创建主要是因为这个原因(虽然也有other 的原因)。文档中提到的性能问题最少。

通过使用 FrameLayout

正如 Rainmaker 已经回答的那样,您可以在您的布局文件夹中使用引用 CoordinatorLayout 布局的 Activity,其中子级将成为框架布局。

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

【讨论】:

    【解决方案2】:

    我已经在应用程序的任何地方实现了类似 open Snackbar 的功能。

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

     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 的情况没有帮助。根本不是我问的!
    • @anoniim 好吧,您的问题是有更好的方法吗? 我认为这个答案完成了 Rahat 的一个。
    • 是的,有没有更好的方法来使用 CoordinatorLayout?。没有它,小吃店将无法滑动,并且无法与其他布局元素很好地搭配。
    • 这就是我需要的+1
    【解决方案3】:

    是的,你可以将你的布局包装成一个协调器布局,比如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2022-01-20
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      相关资源
      最近更新 更多