【问题标题】:Setting CollapsingToolbarLayout image programmatically以编程方式设置 CollapsingToolbarLayout 图像
【发布时间】:2017-02-26 11:00:07
【问题描述】:

我使用的是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);

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

【问题讨论】:

  • 我认为这是因为R.id.toolbar_layoutCoordinatorLayoutid 而不是ImageViewid。将其更改为R.id.header_logo

标签: android android-layout imageview android-toolbar


【解决方案1】:

可能是因为您将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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多