简体   繁体   中英

Status bar's color don't change with Theme.AppCompat.Light

I'm trying to use Material Design in my aplication, and to use the Floating Action Button, I'm using appcompat-v7 library. But, for using this, I must use the theme: "Theme.AppCompat.Light". And I just can't set the background color on the app for API 22. I'm not interesting to make this work on API's 20 or bellow

In Android Documentation, it's says that i just have to user colorPrimary, and colorPrimaryDark on android:Theme.Material https://developer.android.com/intl/pt-br/training/material/theme.html

My style look like this:

<resources>
        <style name="AppTheme" parent="Theme.AppCompat.Light">
            <item name="android:colorPrimary">@color/primary</item>
            <item name="android:statusBarColor">@color/primary</item>
            <item name="android:colorPrimaryDark">@color/primary_dark</item>
            <item name="android:textColorPrimary">@color/primary_text</item>
            <item name="android:textColorSecondary">@color/secondary_text</item>
            <item name="android:colorAccent">@color/accent</item>
            <item name="android:textColor">@color/textColor</item>
        </style>
    </resources>

My MainActivity:

package com.example.rafael.encomendasmaterial.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.rafael.encomendasmaterial.R;

public class MainActivity extends ActionBarActivity {
    private FloatingActionButton myFAB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myFAB = (FloatingActionButton) findViewById(R.id.myFAB);
        setTheme(R.style.AppTheme);
        callCadastroEncoemnda();
    }
    public void callCadastroEncoemnda(){
        myFAB.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CadastroEncomenda.class);
                MainActivity.this.startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

In my case, status bar was not reflecting because i set

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

Removing the lines solved it

Since you're using the support library, certain properties that didn't exist in API 7 should not be android namespaced since they are provided by the library.

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
        <item name="android:textColorSecondary">@color/secondary_text</item>
        <item name="colorAccent">@color/accent</item>
        <item name="android:textColor">@color/textColor</item>
    </style>
</resources>

I Forget to put "android:" before colorPrimary, colorPrimaryDark, and colorAccent. It's working now! Thanks.

<style name="AppTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColorSecondary">@color/secondary_text</item>
    <item name="android:colorAccent">@color/accent</item>
    <item name="android:textColor">@color/textColor</item>
</style>

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