android - 在折叠布局的标题旁边添加图标图像

标签 android android-support-library

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

最佳答案

请看以下代码:

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

并引用承载图片的 fragment ,只需将图片设置为此 ImageView ,您将获得所需的结果:
<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"定义工具栏在滚动时的行为方式。

试试看,加油!

关于android - 在折叠布局的标题旁边添加图标图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066164/

相关文章:

java - 根据Android权限是否禁用显示TextView?

android - 这个类应该是公共(public)的(android.support.v7.internal.widget.ActionBarView.HomeView)

android - 如何使用 Intent 在android中录制音频时设置最大持续时间

java - 如何从单独的类中的对话框返回值

android - 由于数据库更新频繁,借助线程每隔 X 秒启动一次服务

android - BottomNavigationView 始终显示图标和文本标签

android - Android 支持库如何工作?

Android onContentChanged 在 2.3 和 4.2 上

android - Flutter 应用程序从服务器接收 json 文件

java - 如何在 TextView 的文本周围设置边框