【问题标题】:AppBarLayout change offset programmaticallyAppBarLayout 以编程方式更改偏移量
【发布时间】:2016-04-17 08:52:56
【问题描述】:

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

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

我的.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>

需要支持 API 16+

谢谢!

【问题讨论】:

    标签: android android-design-library


    【解决方案1】:

    使用这个:ScrollableAppBar

    您只需将AppBarLayout 替换为:

    <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>
    

    比:

    ScrollableAppBar appBarLayout = (ScrollableAppBar) findViewById(R.id.appbar);
    
    //To give the effect "in the middle" of the image (like gif)
    appBarLayout.collapseToolbar();
    

    输出:

    【讨论】:

    • 嗨,米歇尔,感谢您的回复!它完成了工作!但是,我必须支持 API 16+,我相应地更新了我的问题。
    • 我认为这是不可能的@AdamGhani
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2013-01-24
    相关资源
    最近更新 更多