如何向嵌套在 CollapsingToolbar 中的 ImageView 添加渐变 [英] How to add a gradient to a ImageView nested in a CollapsingToolbar

本文介绍了如何向嵌套在 CollapsingToolbar 中的 ImageView 添加渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有 Material Design 的 Android 应用.我有一个带有 CollapsingToolbarLayout 和 ImageView 的详细视图(到目前为止工作正常).不幸的是,如果有明亮的图像,则标题不可读.到目前为止,我尝试添加渐变(向 imageview 添加渐变),但此解决方案没有由于 CollapsingToolbarLayout 为我工作.在这里你可以看到我的代码:

I'am working on an Android app with material design. I have a detail view with a CollapsingToolbarLayout and a ImageView (works fine so far). Unfortunately the title is not readable if there is a bright image. So far I tried to add a gradient (Add gradient to imageview) but this solution didn't work for me because of the CollapsingToolbarLayout. Here you can see my 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: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>

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

Does anyone know a solution for this issue?

推荐答案

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

Wrap your ImageView in a FrameLayout and add a View with a background:

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

确保你的渐变是这样的:

Make sure your gradient is something like this:

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

这篇关于如何向嵌套在 CollapsingToolbar 中的 ImageView 添加渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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