在折叠布局的标题旁边添加图标图像 [英] Add an icon image beside the title on the collapsing layout

查看:78
本文介绍了在折叠布局的标题旁边添加图标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像添加到折叠布局中.我可以添加文本(标题),但是如何添加图像?

I want to the add the image to the collapsing layout. I can add the text (title) but how do I add the image?

推荐答案

请参见以下代码:

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

并参考托管图片的代码段,只需将图片设置为此imageview,您将获得所需的结果:

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

在您放大图片时,您可以自定义图片的高度,在我的示例中为500dp,也可以对其进行裁剪. 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.

尝试一下,加油!

这篇关于在折叠布局的标题旁边添加图标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆