问题
I want to the add the image to the collapsing layout. I can add the text (title) but how do I add the image?
回答1:
please see the following code:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/user_profile_coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="500dp"
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:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll">
<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="500dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/cover_image_placeholder"
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/ThemeOverlay.AppCompat.Light" />
</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:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Some views -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
And referring to the snippet which hosts a picture, just set a picture to this imageview and you will get the desired result:
<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="500dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/my_image"
app:layout_collapseMode="parallax" />
Obviuosly you can customize the height of the picture when it's expanded, in my example it's 500dp, and cropped as well. app:layout_collapseMode="parallax"
defines the way the toolbar behaves on scrolling.
Try it out, cheers!
来源:https://stackoverflow.com/questions/33066164/add-an-icon-image-beside-the-title-on-the-collapsing-layout