Add image to a collapsingtoolbarlayout

Asked

Viewed 587 times

0

I would like to add a background image in a CollapsingToolbarLayout following this example: Exemplo

I tried it this way:

   <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:background="@drawable/blue_sky"
                app:layout_collapseMode="parallax"/>





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

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

But it’s a feather in the background!

How can I add the image?

  • This example is from some site?

  • yes! of stackoverflow in English, added to illustrate my question

1 answer

1


Use that:

<android.support.design.widget.CoordinatorLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_CollapsingToolbarLayout"
    android:id="@+id/appBar"
    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"
        app:contentScrim="@color/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

       <ImageView
            android:id="@+id/ivPerfilCl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/logo"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"

            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/tbDetalhes"
            android:layout_width="match_parent"
            app:layout_collapseMode="pin"
            app:theme="@style/toobar"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            />

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

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

</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:borderWidth="0dp"
    android:src="@drawable/ic_event_available_white_24dp"
    app:layout_anchor="@id/appBar"
    app:layout_anchorGravity="bottom|right|end"
    android:elevation="2dp"
    android:layout_marginRight="16dp"
    android:id="@+id/FABParticiparDaAtividade"
    app:fabSize="normal"
    />
</android.support.design.widget.CoordinatorLayout>

In mine it works. Look how it turned out. inserir a descrição da imagem aqui

  • So, here do not roll! does not display the image ! I created this screen from the example of Android Studio. You changed something on Themeoverlay.AppCompat.Dark.Actionbar? Thanks!!!

  • Dude if it doesn’t work now, the problem isn’t in the layout

  • Could you tell me the dimension of the background image? Thank you!

  • 1

    The dimension is 504x404.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.