AppBarLayout change offset programmatically

倾然丶 夕夏残阳落幕 提交于 2019-12-10 04:21:20

问题


How can I change the offset of the AppBarLayout programmatically?

I want a certain offset to the AppBarLayout(partially expanded) when the Activity first loads, then the user could expand it more or collapse it. The current behaviour is that it's fully expanded when the Activity first loads.

my.xml

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_collapseParallaxMultiplier="0.7"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/pic"/>

                <View
                    android:id="@+id/overlay"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/overlay"
                    app:layout_collapseMode="pin"/>
            </FrameLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="@string/app_name"/>

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

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

Need to support API 16+

Thanks!


回答1:


Use this: ScrollableAppBar

You just have to replace your AppBarLayout with:

<it.michelelacorte.scrollableappbar.ScrollableAppBar
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="380dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
</it.michelelacorte.scrollableappbar.ScrollableAppBar>

and than:

ScrollableAppBar appBarLayout = (ScrollableAppBar) findViewById(R.id.appbar);

//To give the effect "in the middle" of the image (like gif)
appBarLayout.collapseToolbar();

Output:



来源:https://stackoverflow.com/questions/34748778/appbarlayout-change-offset-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!