2015-06-03 10 views
29

Po przewinięciu ekranu w dół, obraz z Zwijany pasek narzędziowy znika i jest pozostawiony z paskiem narzędziowym z przyciskiem Wstecz, tytułem zawartości i ustawieniami menu. Chcę wiedzieć, jak zmienić kolor tła tego paska narzędzi tylko wtedy, gdy jest on w stanie "zwiniętym".Projektowanie materiałów w Androidzie - jak zmienić kolor tła paska narzędzi po zwinięciu CollapsingToolbarLayout

Akcja mam na myśli jest podobny do tego, w którym Pasek kolor tła zmienia się na zielony:

enter image description here

Poniżej CollapsingToolbarLayout Mam NestedScrollView z CardViews

+2

mogę mieć kod proszę? –

+1

jak stworzyłeś gif? –

Odpowiedz

55

I myślisz, że szukasz adresu app:contentScrim.

<android.support.design.widget.CollapsingToolbarLayout 
    ... 
    app:contentScrim="?attr/colorPrimary"> 
    <!-- Toolbar and ImageView here --> 
</android.support.design.widget.CollapsingToolbarLayout> 
+1

Po obróceniu telefonu ta aplikacja CollapsingToolbarLayout: contentScrim = "? Attr/colorPrimary" powoduje błąd android.view.InflateException Błąd pompowania klasy , inni mówią o zastąpieniu? Attr @ color lub @ dimen itp., Ale nie są rozpoznawani przez aplikację : ... Co ja robię? dzięki –

+0

Czy możesz ELI5 co oznacza Scrim? –

8

Najpierw usuń

app:contentScrim="?attr/colorPrimary"> 

z CollapsingToolbarLayout

Dodaj biblioteka

compile 'com.android.support:palette-v7:23.2.1' 

I dodać poniżej kodu w kodzie java

Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ny); 
    Palette.generateAsync(bitmap, 
      new Palette.PaletteAsyncListener() { 
       @Override 
       public void onGenerated(Palette palette) { 
        Palette.Swatch vibrant = 
          palette.getVibrantSwatch(); 
        int mutedColor = palette.getVibrantSwatch().getRgb(); 
        if (vibrant != null) { 
         // If we have a vibrant color 
         // update the title TextView 
         collapseToolbar.setBackgroundColor(mutedColor); 
         // mutedColor = palette.getMutedColor(R.attr.colorPrimary); 
         collapseToolbar.setStatusBarScrimColor(palette.getDarkMutedColor(mutedColor)); 
         collapseToolbar.setContentScrimColor(palette.getMutedColor(mutedColor)); 

        } 
       } 
      }); 
+0

'generateAsync' jest przestarzałe i nie zmienia się po mojej stronie, chociaż po usunięciu' app: contentScrim = "? Attr/colorPrimary" ', jakakolwiek pomoc? – blueware

5

Wystarczy użyć atrybutu CollapsingToolbarLayout XML contentScrim, aby ustawić kolor tła Toolbar, gdy jest w trybie collapsed.

app:contentScrim="YOUR_TOOLBAR_COLOR"

Oto przykład:

<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:layout_scrollFlags="scroll|exitUntilCollapsed"> 

    <ImageView 
     android:id="@+id/img_group_photo" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:scaleType="centerCrop" 
     app:layout_collapseMode="parallax" /> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/anim_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> 

nadzieję, że pomoże ~

-1
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
      R.drawable.header); 

    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { 
     @SuppressWarnings("ResourceType") 

     @Override 
     public void onGenerated(Palette palette) { 
      Palette.Swatch vibrant = 
        palette.getVibrantSwatch(); 

      if (vibrant != null) { 

       collapsingToolbar.setBackgroundColor(getResources().getColor(R.color.cpb_blue)); 
       collapsingToolbar.setStatusBarScrimColor(getResources().getColor(R.color.cpb_blue)); 
       collapsingToolbar.setContentScrimColor(getResources().getColor(R.color.cpb_blue)); 

      } 
     } 

    }); 
Powiązane problemy