2017-08-17 31 views
0

我试图改变动作条的颜色在我的“AppBar布局”程序,通过这个代码:改变“动作条”色不给正确的颜色

toolbar.setBackgroundColor(R.color.hidden_bars); //#464445 

但每次我用不同颜色的结束,在colorPrimary(#48bee6)

这里更暗的版本是我的xml:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/id_appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="enterAlwaysCollapsed" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     style="@style/WenoTabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/tab_bar_height" 
     android:layout_marginTop="1dp" 
     android:background="@color/white" 
     app:tabGravity="fill" 
     app:tabMode="fixed" /> 

</android.support.design.widget.AppBarLayout> 
+0

check colorPrimary和colorPrimaryDark在您的应用程序主题将出现在style.xml – Reena

回答

1

这是setBackgroundColor(int)一个普遍的误解。传递到setBackgroundColor的int值应该是颜色代码而不是颜色资源ID。您应该首先通过id获取颜色代码。

// Use this instead of context if you are in activity 
int color = ContextCompat.getColor(context, R.color.your_color); 
toolbar.setBackgroundColor(color); 
+0

非常感谢,真的很有用 – Abuzaid