我正在尝试以编程方式设置AppBarLayout的主要颜色。 XML布局是AndroidStudio的滚动示例:
<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>
在这个活动中,我希望AppBarLayout内的所有项目都有黄色背景,因此我设置:
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);
}
除了当我滚动工具栏时(在FAB消失的确切位置),一切都正常。此时,工具栏的颜色仍然是默认的原始颜色(蓝色,而不是黄色),就像这张图片中所示:
所以,两个问题:
- 我是否错过了一个方法调用?
- 有没有关于调试这些情况的提示?在Android设备监视器的视图层次结构转储中,我无法确定哪个视图被着色为这种颜色。