简体   繁体   中英

Why can't I change background color of toolbar

I'm changing toolbar color like this:

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>

and MainActivity.java

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

I can change color colorPrimaryDark and textColorPrimary but can not change colorPrimary, why?

I can change toolbar color like this:

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

but cant change using style also why after removing setActionBar(toolbar); the text and title overflow menu gone why?

From XML

android:background:"your color"

From code

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

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

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

put these line

 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代替

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM