AppBarLayout以编程方式更改偏移量 [英] AppBarLayout change offset programmatically

查看:133
本文介绍了AppBarLayout以编程方式更改偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式更改AppBarLayout的偏移量?

我希望在Activity首次加载时对AppBarLayout(部分展开)有一定的偏移量,然后用户可以对其进行更多扩展或折叠.当前的行为是,当Activity首次加载时,它已完全展开.

my.xml

 < android.support.design.widget.AppBarLayoutandroid: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.CollapsingToolbarLayoutandroid: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">< FrameLayoutandroid:layout_width ="match_parent"android:layout_height ="match_parent"app:layout_collapseMode ="parallax">< ImageViewandroid:id ="@ + id/image"android:layout_width ="match_parent"android:layout_height ="match_parent"android:scaleType ="centerCrop"android:src ="@ drawable/pic"/><查看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.Toolbarandroid: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> 

需要支持API 16 +

谢谢!

解决方案

使用此:

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!

解决方案

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:

这篇关于AppBarLayout以编程方式更改偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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