统筹布局与工具栏的片段或活动 [英] Coordinator Layout with Toolbar in Fragments or Activity

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

问题描述

随着新的设计库有几个新的布局,改变了很多工具栏可以的行为,如果开发商愿意的。由于不同的片段具有不同的行为和目标,例如一个像册片段与塌陷工具栏示出了重要的照片,或不只是不需要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。如果我离开的工具栏中的活动孤单,我要对在每个片段中的每个类型的行为定义的多种布局。什么是最好的方法?

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?

推荐答案

对于我来说,这听起来太不可思议有appbar和工具条中的每个片段。所以,我选择了与工具栏上的活动单appbar。

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.

让我们假设,你的默认行为是应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior

Lets assume, that your default behaviour is 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>

和在您不希望实施应用程序的每个片段:layout_behavior =@字符串/ appbar_scrolling_view_behavior,你将不得不重写 onAttach onDetach 的方法,这将改变你的行为的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天全站免登陆