以编程方式设置 CollapsingToolbarLayout 图像

Setting CollapsingToolbarLayout image programmatically

我正在使用 ScrollingActivity,它带有 CollapsingToolbarLayout。我想向此布局添加一个 ImageView,并能够以编程方式切换图像,但它似乎没有做任何事情。

XML 我的布局:

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

        <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/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/header_logo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            android:layout_gravity="center"
            app:layout_collapseMode="parallax"
            />

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

当我尝试在事件中执行以下操作时:

ImageView im = (ImageView)findViewById(R.id.toolbar_layout);
im.setBitmap(image_here);

但是当我触发这个事件时,没有任何反应。我知道我的图像被正确解析了,因为事实上我没有从中得到任何异常;在整个过程中我没有得到任何例外。我在这里做错了什么吗?有没有办法刷新此工具栏以更新图像?

可能是因为您将 Toolbar 的布局 ID 调用为 ImageView

尝试更改

ImageView im = (ImageView)findViewById(R.id.toolbar_layout);
im.setBitmap(image_here);

ImageView im = (ImageView)findViewById(R.id.header_logo);
im.setBitmap(image_here);