简体   繁体   中英

Toolbar turns white (style/color not applied) in Android 6.0 Marshmallow

When we started testing our app on Android 6.0 devices the Toolbar (support action bar) is white. It's not using the colors we specified in the styles.xml. This has worked for all pre-marshmallow devices. Anyone knows why that might be? I couldn't find anything about it on Google.

Some code:

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
}

activity_main.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"
tools:context="com.example.swekjaks.actionbartest.MainActivity">
<include android:id="@+id/toolbar"
         layout="@layout/tool_bar" />

</RelativeLayout>

tool_bar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:background="@color/PrimaryColor"
                               android:foreground="@color/white"
                               android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">

<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionBarStyle" tools:targetApi="11">@style/RelacomActionBar</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/RelacomActionBar</item>

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!-- ActionBar styles -->
<style name="RelacomActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/colorPrimary</item>

    <!-- Support library compatibility -->
    <item name="background">@color/colorPrimary</item>
</style>

</resources>

Copy the custom styles and colors to styles.xml (v21) as well and it will work fine.

在此处输入图片说明

The path for the styles.xml (v21) is:

\app\src\main\res\values-v21\styles.xml

I think its a bug in the design library. I'm using libraries 23.3.0. and the bug still occurs. Workaround for this is adding View that takes nearly no space and can be invisible. See the structure below.

<android.support.v4.widget.NestedScrollView
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.Toolbar
        app:layout_scrollFlags="scroll|enterAlways" />

    <android.support.design.widget.TabLayout
        app:layout_scrollFlags="scroll|enterAlways" />

    <View
        android:layout_width="match_parent"
        android:layout_height=".3dp"
        android:visibility="visible"/>

</android.support.design.widget.AppBarLayout>

this worked for me. let people know if it works for you. for more details please refer https://code.google.com/p/android/issues/detail?id=178037

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