片段或活动中带有工具栏的协调器布局 [英] Coordinator Layout with Toolbar in Fragments or Activity

查看:25
本文介绍了片段或活动中带有工具栏的协调器布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新的设计库中有几个新的布局,如果开发人员愿意,这些布局会改变工具栏的行为方式.由于不同的片段具有不同的行为和目标,例如带有显示重要照片的折叠工具栏的图库片段,或者没有滚动视图的片段,不需要 appbarlayout 来隐藏工具栏,在活动中使用单个工具栏可以证明很难.

With the new design library there are several new layouts that change a lot how the toolbar can behave if the developer so wishes. Since different fragments have different behaviors and objectives, for example a gallery fragment with a collapsing toolbar showing an important photo, or a fragment without a scrollview that just doesn't need the appbarlayout for hiding the toolbar, having a single toolbar in the activity can prove difficult.

因此,我应该将工具栏移动到每个片段吗?如果是这样,我必须在每次显示片段时设置 supportActionBar,并且还必须引用片段中的活动,这使片段的独立性质无效.如果我将工具栏单独留在 Activity 中,我必须为每个片段中的每种行为定义多个布局.最好的方法是什么?

So with this, should I move the toolbar to each fragment? If so, I have to set the supportActionBar each time I show a fragment and also have a reference of the activity in the fragment which nullifies the independent nature of fragments. If I leave the toolbar in the Activity alone, I have to have multiple layouts defined for each type of behavior in each fragment. What would be the best approach?

推荐答案

对我来说,在每个片段中都有应用栏和工具栏听起来太奇怪了.所以我选择在活动中使用带有工具栏的单个应用栏.

As for me it sounds too weird to have appbar and toolbar in each fragment. So I've chosen to have single appbar with toolbar in activity.

要使用 CoordinatorLayout 解决该问题,您必须设置 FrameLayout(或任何其他布局)的不同行为,这些行为应该包含来自您想要覆盖默认行为的每个片段的片段.

To solve that issue with CoordinatorLayout you will have to set different behaviour of your FrameLayout (or any other Layout) that supposed to hold fragments from each fragment that you want to override default behaviour.

假设您的默认行为是 app:layout_behavior="@string/appbar_scrolling_view_behavior"

然后在你的 fragment_activity_layout.xml 你可能有这样的东西:

Then in your fragment_activity_layout.xml you may have something like that:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/dashboard_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.Toolbar"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/dashboard_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

并且在您不希望实现 app:layout_behavior="@string/appbar_scrolling_view_behavior" 的每个片段中,您必须覆盖 onAttachonDetach 将改变 FrameLayout 行为的方法:

And in each fragment you wish not to implement app:layout_behavior="@string/appbar_scrolling_view_behavior" you will have to override onAttach and onDetach methods that will change behaviour of your FrameLayout:

CoordinatorLayout.Behavior behavior;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    if(behavior != null)
        return;

    FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();

    behavior = params.getBehavior();
    params.setBehavior(null);

}

@Override
public void onDetach() {
    super.onDetach();
    if(behavior == null)
        return;

    FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();

    params.setBehavior(behavior);

    layout.setLayoutParams(params);

    behavior = null;
}

在此之后 CoordinatorLayout 不会折叠 appbar 等,并允许片段布局为全高.

After that CoordinatorLayout won't collapse appbar, etc. and will allow fragment layouts to be full-height.

这篇关于片段或活动中带有工具栏的协调器布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆