Set navigationBar color on Android Lollipop with F

2019-05-17 00:03发布

问题:

Is there a way to set the navigationbar color in a fullscreen activity?

if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setNavigationBarColor(getResources().getColor(R.color.Theme_color));

And this line in my theme-style:

<item name="android:navigationBarColor" tools:targetApi="21">#E64A19</item>

Both resulting in the same transparant navbar above my activities layout...

Code:

Acitivity :

@Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //Remove title bar
                this.requestWindowFeature(Window.FEATURE_NO_TITLE);

                //Remove notification bar
                this.getWindow()
    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                setContentView(R.layout.activity_account);

                if (getResources().getBoolean(R.bool.portrait_only)) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }

                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow()
        .setNavigationBarColor(getResources().getColor(R.color.Theme_color));
                }


                //other code ... (irrelevant)

Theme:

<style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@null</item>
        <item name="metaButtonBarStyle">@style/ButtonBar</item>
        <item name="metaButtonBarButtonStyle">@style/ButtonBarButton</item>
        <item name="android:navigationBarColor" tools:targetApi="21">#E64A19</item>
</style>

The result is a black navigationbar. But I want a orange (#E64A19) navigation-bar.

Result:

回答1:

My solution:

The problem lied with the generated code (XML theme) once I'd deleted the parent="android:Theme.NoTitleBar"attribute from my theme style (and everything else except <item name="android:navigationBarColor" tools:targetApi="21">#E64A19</item>) my navbar became orange!

Result:



回答2:

Check this library : SystemBarTint.

This can be useful to you.



回答3:

Place this is your values-v21/styles.xml, to enable this on Lollipop