2016-05-26 7 views
9

Właśnie zaczynam uczyć się Androida, uczę się dodawać AppBar. Ale jeśli użyję Relative Layout jako root, to Pasek stanu nie użyje koloru "colorPrimaryDark" zdefiniowanego w motywie. Ale zmiana układu na CoordinatorLayout sprawi, że zadziała.Pasek stanu Kolor nie zmienia się z układem względnym jako element główny

Czy ktoś może wyjaśnić różnicę? czy jest to wymagane, aby użyć układu CoordinatorLayout?

Aktywność zwiększa AppCompatActivity.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:fitsSystemWindows="true" 
    tools:context="com.example.ankit.designdemo.MainActivity"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    </android.support.v7.widget.Toolbar> 
</RelativeLayout> 

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true" 
    tools:context="com.example.ankit.designdemo.MainActivity"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.CoordinatorLayout> 

enter image description here

<!-- 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" /> 


public class MainActivity extends AppCompatActivity { 

    private DrawerLayout mDrawerLayout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom_activity); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); 
     actionBar.setDisplayHomeAsUpEnabled(true); 


    } 

    @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; 
    } 


} 
+0

Czy możesz pokazać gdzie zdefiniowane style 'colorPrimaryDark'? – hrskrs

+0

@hrskrs Dodano do pytania .. – Ankit

+0

Czy jesteś pewien, że twoja strona stylów jest na 'values-v21'? – hrskrs

Odpowiedz

19

W v21/styles.xml prawdopodobnie masz ten wiersz kodu:

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

A jak działają na API 23, deklarowane style overrided od te w v21/styles.xml.

Jako rozwiązanie:

prostu usunąć tę linię i będzie działać dobrze

+0

na miejscu, Mój problem został rozwiązany i jak się uczę, czy możesz dodać wyjaśnienia na temat nadpisuję ... Miałem wrażenie, że v21/styles.xml będzie używał tylko w API 23 .. Również dlaczego rzeczy pracowały z CoordinatorLayout, które będą dobrym dodatkiem, a także – Ankit

+0

@Ankit O stylach, które można przeczytać [tutaj] (https : //developer.android.com/guide/topics/ui/themes.html) o tym, jak działają w oparciu o platformę. Informacje o 'CoordinatorLayout': działa z powodu' android: fitsSystemWindows = "true" ', jeśli go usuniesz, pokaże ci to samo zachowanie. – hrskrs

+1

Teraz widzę ... więc v21/styles.xml będzie działać, gdy API 21 lub nowszy .. Dzięki za pomoc .. – Ankit

Powiązane problemy