简体   繁体   中英

Status bar color won't change with AppCompatActivity

I'm using Android 5 (emulator) for testing my application, but I don't know why the status bar color won't change.

styles.xml:

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#0000ff</item>
    <item name="colorPrimaryDark">#8ef8ef</item>
    <item name="colorAccent">#000fff</item>
    <item name="android:statusBarColor">#8ef8ef</item>
</style>

</resources>

Java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Tested also on Android 6 (emulator), Toolbar is #0000ff but status bar is still black...

I am not sure but it may be problem regarding to your emulator or device

please confirm that your AppTheme style is not in style(v21). if you are using device or emulator below version of 21.

On API 22 (SGS6) it works correctly. Your problem may be cause of the following BUG . Did you try update your system images for API 21 and API 22?

Use below code for style

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/red4</item>
    <item name="android:colorPrimaryDark">@color/red10</item>

</style>

Also dont forgot to give style in your manifest file application tag.

android:theme="@style/AppTheme">

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