android - 如何向嵌套在 CollapsingToolbar 中的 ImageView 添加渐变

标签 android imageview android-support-library material-design android-collapsingtoolbarlayout

我正在开发一款采用 Material Design 的 Android 应用。我有一个带有 CollapsingToolbarLayout 和 ImageView 的详细 View (到目前为止工作正常)。不幸的是,如果有明亮的图像,标题就不可读了。 screenshot of this issue 到目前为止,我尝试添加渐变 ( Add gradient to imageview ),但由于 CollapsingToolbarLayout,此解决方案对我不起作用。 在这里你可以看到我的代码:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_height="256dp"
    android:layout_width="match_parent"
    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:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="@color/primary"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginEnd="50dp"
        app:expandedTitleMarginStart="50dp"
        >


        <!--<View
            android:background="@drawable/actionbar_gradient_dark"
        />-->
            <ImageView
                android:id="@+id/backimg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                android:scaleType="centerCrop"
                app:layout_scrollFlags="scroll|enterAlways"
                />

        <android.support.v7.widget.Toolbar
            android:id="@+id/detailToolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:layout_collapseMode="pin"
            app:layout_collapseParallaxMultiplier="0.7"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

有人知道这个问题的解决方案吗?

最佳答案

将您的 ImageView 包裹在 FrameLayout 中并添加一个带有背景的 View :

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/backimg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax"
        android:scaleType="centerCrop"
        android:src="@drawable/image"
        app:layout_scrollFlags="scroll|enterAlways" />
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/actionbar_gradient_dark" />
</FrameLayout>

确保你的渐变是这样的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:angle="90"
        android:endColor="@android:color/transparent"
        android:startColor="@android:color/black" />
    <corners android:radius="0dp" />
</shape>

关于android - 如何向嵌套在 CollapsingToolbar 中的 ImageView 添加渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30902812/

相关文章:

android - Android 工具栏后退按钮上的动画

android - 如何将图像下载器移动到 Android 上的单独线程中?

android - fragment 标准过渡没有动画

android - 图片不清晰 - picasso Android

android - 如何在RelativeLayout中将ImageButton放置在ImageView上

android - 资源链接失败,错误 : resource android:attr/dialogCornerRadius not found

android - fragment 必须是公共(public)静态类才能从实例状态正确地重新创建

android - logcat中的数百个随机错误

使用 Glide 的圆形个人资料图片的 Android 边框

android - gradle.properties vs buildscript ext 哪个更好