2015-11-13 19 views
5

Próbuję programowo ustawić kolor podstawowy AppBarLayout. Układ XML jest AndroidStudio za Przewijanie próbki:Ustaw kolor scrim programowo

<android.support.design.widget.AppBarLayout android:id="@+id/app_bar" 
    android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height" 
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" 
     android:fitsSystemWindows="true" android:layout_width="match_parent" 
     android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     app:contentScrim="?attr/colorPrimary"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" 
      app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

oraz w działalności, chcę wszystkie elementy wewnątrz AppBarLayout mieć żółtym tle, więc jestem Otoczenie:

int barColor = Color.parseColor("#FFC107"); 
AppBarLayout barLayout = (AppBarLayout) this.findViewById(R.id.app_bar); 
if (barLayout != null) { 
    barLayout.setBackgroundColor(barColor); 
} 
toolbar.setBackgroundColor(barColor); 

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) this.findViewById(R.id.toolbar_layout); 
if (collapsingToolbarLayout != null) { 
    collapsingToolbarLayout.setBackgroundColor(barColor); 
    collapsingToolbarLayout.setContentScrimColor(barColor); 
} 

wszystko działa poprawnie, z wyjątkiem sytuacji, gdy jestem w połowie przewijania paska narzędzi (dokładnie w tym miejscu, w którym zniknie FAB). W tym stanie, kolor paska narzędzi jest nadal domyślny kolor podstawowy (niebieski, nie żółty), tak jak na tym zdjęciu:

enter image description here

Więc dwa pytania:

  • jestem brakuje metody połączenie?
  • Jakieś wskazówki dotyczące debugowania tych scenariuszy? W widoku hierarchii widoku urządzenia Android Monitor nie mogę stwierdzić, który z nich jest podbarwiony tym kolorem.

Odpowiedz

12

miałem ten sam problem, trzeba ustawić siatka kolor StatusBar także:

int red = ContextCompat.getColor(activity, R.color.red); 
    collapsingToolbar.setBackgroundColor(red); 
    collapsingToolbar.setContentScrimColor(red); 
    collapsingToolbar.setStatusBarScrimColor(red); 

można nawet uzyskać kolor bezpośrednio przy użyciu:

collapsingToolbar.setBackgroundResource(R.color.red); 
    collapsingToolbar.setContentScrimResource(R.color.red); 
    collapsingToolbar.setStatusBarScrimResource(R.color.red); 
+0

dzieło doskonałe, dziękuję you –

+0

collapsingToolbar.setStatusBarScrimColor (czerwony) nie zmienia koloru paska stanu. –

Powiązane problemy