如何在较旧的API上实现windowDrawSystemBarBackgrounds和statusBarColor,即v19?

时间:2016-10-19 22:00:30

标签: android xml

我的主要活动扩展了AppCompatActivity,我正在使用支持工具栏:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

我想使用windowsDrawSystemBarBackground和statusBarColor属性为工具栏着色,这在&gt; = v21中可以正常工作:

Base Application Theme

但是当试图在第19行上运行时,所有的地狱都会破裂,即:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.toadlybroodledev.sublist/ca.toadlybroodledev.sublist.ActivityMain}: java.lang.NullPointerException: println needs a message

似乎所有Log错误都传递了空消息?对此感到非常困惑。

这是我的主要活动布局,它还有一个扩展的DrawerLayout作为它的根目录:

<ca.toadlybroodledev.sublist.DrawerCustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- This LinearLayout represents the contents of the screen  -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- The ActionBar displayed at the top when drawer is shown -->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

    <!-- The main content view where fragments are loaded -->
    <FrameLayout
        android:id="@+id/placeholder_for_fragments"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
    android:id="@+id/nvView"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorPrimaryDark"
    app:itemIconTint="@drawable/drawer_item_color"
    app:itemTextColor="@drawable/drawer_item_color"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu" />

</ca.toadlybroodledev.sublist.DrawerCustomLayout>

我尝试创建一个单独的样式(v21)资源,该资源适用于API的&gt; = 21,但不能解决潜在的向后兼容性问题。我也尝试从工具栏中删除“android:theme =”@ style / ThemeOverlay.AppCompat.Dark.ActionBar“,没有区别。

任何帮助都会非常感激,正确实现版本化的Andoid样式和主题由于某种原因总是让我无法...

[编辑1]如果有人感兴趣,我终于可以通过将其添加到构建文件而无需缩小来构建v19:

dexOptions { javaMaxHeapSize "2g" }

这是我现在得到的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.RuntimeException: Unable to start activity ComponentInfo{...ActMain}: java.lang.NullPointerException: println needs a message
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                  at android.app.ActivityThread.access$800(ActivityThread.java:135)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5017)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.NullPointerException: println needs a message
                  at android.util.Log.println_native(Native Method)
                  at android.util.Log.d(Log.java:139)
                  at ca.toadlybroodledev.sublist.ActMain.updateColors(ActMain.java:404)
                  at ca.toadlybroodledev.sublist.DrawerCustomLayout.onFinishInflate(DrawerCustomLayout.java:50)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:764)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                  at ca.toadlybroodledev.sublist.ActMain.onCreate(ActMain.java:77)

1 个答案:

答案 0 :(得分:0)

您只能从Lollipop更改状态栏的颜色 如果你想改变Api&gt; = 19状态栏的颜色,你需要在值的样式-v19中检查你需要特定版本的文件(olders):

<?xml version="1.0" encoding="utf-8"?>
<resources>

        <style name="AppTheme" parent="MaterialDrawerTheme.Light">
                <!-- Customize your theme here. -->
                <item name="android:windowNoTitle">true</item>
                <item name="android:windowTranslucentStatus">true</item>
                .....
        </style>
</resources>