10

我正在使用材质设计处理Android应用程序。我有一个CollapsingToolbarLayout和一个ImageView的细节视图(到目前为止工作正常)。不幸的是,如果有明亮的图像,标题是不可读的。 screenshot of this issue 到目前为止,我尝试添加一个渐变(Add gradient to imageview),但由于CollapsingToolbarLayout,此解决方案对我无效。 在这里你可以看到我的代码:如何将渐变添加到CollapsingToolbar中的ImageView嵌套

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

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

+0

斗在应用渐变只图像具有与字体颜色相似? –

+0

我使用与我的标题具有相似颜色的所有图片的渐变。 – Jobs

回答

19

包装你的ImageView中的FrameLayout,并与背景添加视图:

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

谢谢:)它的工作! – Jobs

+0

您是否仅将该渐变应用于与字体颜色相似的图像? –