如何在可折叠工具栏布局中设置重力顶部

时间:2017-06-10 12:03:07

标签: android android-tablayout android-collapsingtoolbarlayout

我在折叠后面对折叠工具栏中的一个问题,工具栏与tablayout重叠。如果我不滚动它工作正常但如果我向上滚动它们两者都重叠,我动态更改工具栏折叠模式碎片。enter image description here

代码:

  private void setToolbarPin(Toolbar toolbar)
    {

        ViewGroup.LayoutParams params = toolbar.getLayoutParams();
        CollapsingToolbarLayout.LayoutParams newParams;
        if (params instanceof CollapsingToolbarLayout.LayoutParams) {
            newParams = (CollapsingToolbarLayout.LayoutParams)params;
        } else {
            newParams = new CollapsingToolbarLayout.LayoutParams(params);
        }

        newParams.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_OFF);
        newParams.gravity = Gravity.TOP;
        toolbar.setLayoutParams(newParams);






      //  toolbar.requestLayout();

    }
}
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/collapsing_toolbar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            android:fitsSystemWindows="true"
            >


            <View
                android:id="@+id/customView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax"
                ></View>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:paddingTop="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_gravity="bottom"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:layout_gravity="top"
                android:layout_alignParentTop="true"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                />
        </android.support.design.widget.CollapsingToolbarLayout>



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



    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >


    </FrameLayout>


    <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="@dimen/fab_margin"
        android:src="@drawable/ic_done" />

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

1 个答案:

答案 0 :(得分:0)

试试这段代码:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:expanded="true"
    android:fitsSystemWindows="true"
    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"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways">
    </android.support.v7.widget.Toolbar>
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextColor="@android:color/white"
        app:tabGravity="fill"
        app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>