Collapsible Toolbar:设置工具栏在onCreate中应该折叠多少

Collapsible Toolbar: Set how much the toolbar should be collapsed in onCreate

我正在创建一个 ListView 及其对应的 DetailView 应用程序。 我的 ListView 有项目,如果点击这些项目会将用户带到 DetailViewActivity

DetailViewActivity 上,我实现了可折叠工具栏。 现在,每次打开 DetailViewActivity 时,都会在可折叠工具栏中的 ImageView 上设置不同的图像(具有不同的尺寸)。

我希望Toolbar默认打开到一定高度(比如256dp),但是如果图片高度大于那个高度,用户应该可以下拉查看图像的其余部分。 (就像 Whatsapp)

每次打开 activity 时,我都设法以编程方式设置了 Toolbar 的高度,但问题是,Toolbar 总是完全展开。所以如果图片比较大,工具栏默认就很大。无论图像的高度如何,我都希望它折叠到 256dp。


我的布局代码是:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/rootLayout"
        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="@dimen/app_bar_height"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/background_navdrawer"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7"/>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="130dp"
                    android:background="@drawable/gradient_header_background"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.1"/>

                <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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>


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

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


        <android.support.v4.widget.NestedScrollView
            android:id="@+id/detail_nested_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <FrameLayout
                android:id="@+id/detail_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:ignore="MergeRootFrame"/>

        </android.support.v4.widget.NestedScrollView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="12dp"
            android:orientation="vertical"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom">

            <View
                android:id="@+id/toolbar_shadow_transparent"
                android:layout_width="match_parent"
                android:layout_height="@dimen/toolbar_elevation"
                android:background="@color/transparent"/>

            <View
                android:id="@+id/toolbar_shadow"
                android:layout_width="match_parent"
                android:layout_height="@dimen/toolbar_elevation"
                android:background="@drawable/dropshadow"/>
        </LinearLayout>


        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/action_edit"
            style="@style/MenuButtonsStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:src="@drawable/ic_edit"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom|right|end"/>

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

    <ImageView
        android:id="@+id/detail_back_arrow_land"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/course_name_textview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone"/>

</RelativeLayout>

在我的 Activity 中,我找到了高度并将其设置为 Toolbar,如下所示:

appBar = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
lp.height = my_bitmap.getHeight();
DetailActivity.appBar.setLayoutParams(lp);
DetailActivity.mImageView.setImageBitmap(my_bitmap);

我附上截图是为了让我的观点更清楚。

这正是每次 activity 启动时我希望工具栏的高度:

这就是我从我的代码中得到的:

现在,我可以在代码中将高度硬编码为 256dp,但用户将无法向下滚动以查看图像的其余部分。请提出建议。

感谢您的回复。 任何回应都可以让我开始

终于找到解决办法了。收到新图像后,我将位图作为参数传递给 expandToolbar() 方法。 heightDp 参数指定 View 的初始滚动高度。 感谢 Tuấn Trần Anh

,我从 那里得到了灵感
public static void expandToolbar(Bitmap bmp, int heightDp) {
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
    AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
    behavior.setTopAndBottomOffset(0);
    behavior.onNestedPreScroll(rootLayout, appBar, null, 0, bmp.getHeight() - heightDp, new int[2]);
    params.setBehavior(behavior);
    DetailActivity.appBar.setLayoutParams(params);
}

希望这对某人有所帮助。