为什么我不能更改工具栏的背景颜色

Why can't I change background color of toolbar

我正在像这样更改工具栏颜色:

MainActivity.xml

    <Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></Toolbar>

styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">#0000ff</item>
    <item name="android:colorPrimaryDark">#ff0000</item>
    <item name="android:textColorPrimary">#fff</item>
    </style>

和MainActivity.java

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setActionBar(toolbar);

我可以改变颜色 colorPrimaryDark 和 textColorPrimary 但不能改变 colorPrimary,为什么?

我可以像这样更改工具栏颜色:

toolbar.setBackgroundColor(Color.parseColor("#0000ff"));

但无法更改使用样式也为什么在删除 setActionBar(toolbar) 之后;为什么文本和标题溢出菜单不见了?

您可以从父主题名称中删除 NoActionBar

放这些行

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 ActionBar actionBar = getSupportActionBar();

            actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));

改用这个主题Theme.AppCompat.Light.NoActionBar

来自XML

android:background:"your color"

来自代码

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
ActionBar actionBar = getSupportActionBar();

actionBar.setBackgroundDrawable(Color.parse("your color"));