如何渐变添加到嵌套在一个CollapsingToolbar一个ImageView的嵌套、CollapsingToolbar、ImageView

2023-09-08 09:53:46 作者:不用拉手,我自己能走

我'的工作与材料设计的Andr​​oid应用程序。我有一个CollapsingToolbarLayout和ImageView的(正常工作为止)的详细视图。不幸的是,标题是不可读,如果有一个明亮的图像。你可以找到这个链接的 https://m.xsw88.com/allimgs/daicuo/20230908/277.png?dl=0 (我不允许尚未发布链接)

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. You can find a screenshot of this issue under this link https://m.xsw88.com/allimgs/daicuo/20230908/277.png?dl=0 (I am not allowed yet to post links)

到目前为止,我尝试添加一个渐变(添加梯度的ImageView ),但这种解决方案对我来说,因为CollapsingToolbarLayout没有工作。 在这里,你可以看到我的code:

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,并添加一个背景查看:

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>