12

Ich habe ein Layout, mit dem die Hintergrundfarbe der Statusleiste basierend auf colorPrimaryDark aktualisiert wurde.So erhalten Sie die Hintergrundfarbe für den Hintergrund color_PrimaryDark

Das funktionierte gut, wenn das Root-Layout Layout ein CoordinatorLayout war, aber wenn ich es auf ein LinearLayout der Hintergrund Statusleiste eingeschaltet wird nicht mehr aktualisiert.

Die Quelle für das Layout und ein Screenshot sind unten eingefügt. Ein Beispiel für ein Layout, das ordnungsgemäß funktioniert, ist ebenfalls aufgeführt.

Vielen Dank!

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".churches.ChurchesActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 
    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 

     <FrameLayout 
      android:id="@+id/contentFrame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </android.support.design.widget.CoordinatorLayout> 

</LinearLayout> 

styles.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> 

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

Arten-v21.xml

<resources> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style> 

Bar

Der Status ist nicht colorPrimaryDark

Status Bar is not colorPrimaryDark

Statusleiste ist colorPrimaryDark

Status Bar is colorPrimaryDark

+0

zeigen Sie uns Ihren Stil.xml –

Antwort

16

Wenn Stile-v21.xml Posting fand ich, dass Android: statusBarColor auf transparent gesetzt wurde:

<item name="android:statusBarColor">@android:color/transparent</item> 

Ändern android: statusBarColor zu colorPrimaryDark es fixiert. Vielen Dank!

<item name="android:statusBarColor">@color/colorPrimaryDark</item> 

nicht sicher, warum statusBarColor ins Spiel kam, nachdem er von einem CoordinatorLayout zu einem Linearlayout wechseln. Vielen Dank!

+1

CoordinatorLayout zeichnet einen Hintergrund der Statusleiste, wenn es aus Systemfenstern einen oberen Einschub hat. Standardmäßig verwendet es 'colorPrimaryDark' als Zeichen zum Zeichnen. – Karakuri

+0

Ich weiß nicht, wie ich das herausgefunden hätte! Danke für die Abklärung! – dazza5000

+0

Nicht für mich arbeiten .... – GvSharma

0

Versuchen Sie, diese android hinzuzufügen: fitsSystemWindows = "true" Eigenschaft für Ihre Linear Layout und zu sehen.

+0

Ich versuchte dies, aber aus irgendeinem Grund Android: FitsSystemWindows = "True" hatte nicht den gleichen Effekt, wenn auf ein LinearLayout wie es angewendet wurde, wenn es auf CoordinateLayout angewendet wurde. Guter Vorschlag! Vielen Dank! – dazza5000

+0

@Shiva, das ist nicht die richtige Antwort, Einstellung 'android: fitsSystemWindows =" true "' wird nur 'LinearLayout' hinzufügen fügen einige paddingTop, wird es nicht' LinearLayout' zeichnen Farbe auf der Statusleiste. – neevek

0

Fügen Sie in Ihrem Design <item name="android:navigationBarColor">?attr/colorPrimaryDark</item> hinzu, oder rufen Sie window.setNavigationBarColor(@ColorInt int color) programmgesteuert auf. Aber nehmen Sie zur Kenntnis, dass navigationBarColor seit Android Lollipop eingeführt wird (API 21), die navigationBarColor bedeutet wird nicht unterstützt, wenn API < 21.

1

die Sie interessieren in Aktivität vor Set Inhalt Ansicht

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
     getWindow().setStatusBarColor(getFactorColor(getResources().getColor(R.color.action_bar_color), 0.4f)); 
} 

wo getFactorColor Methode ist

public static int getFactorColor(int color, float factor) { 
    float[] hsv = new float[3]; 
    Color.colorToHSV(color, hsv); 
    hsv[2] *= factor; 
    color = Color.HSVToColor(hsv); 
    return color; 
} 
0

Wenn Sie nicht alle diese versuchen werden. Machen Sie den Cache ungültig und starten Sie die Projekte neu. definitiv wird das funktionieren.