正确的造型方式Android Lollipop 5.0工具栏

时间:2014-11-09 01:49:04

标签: android styling android-5.0-lollipop android-toolbar

我一直在敲打这个已经有一段时间没有运气了。 我正在使用我的应用程序中的android.support.v7.widget.Toolbar(支持Android API 11及更高版本),并希望对其进行样式设置。以下是我的尝试方法:

我的活动布局XML包含:

        <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="?attr/foo" />

res / values / attr.xml 包含(这是我定义属性的地方):

        <attr name="foo" format="reference" />

RES /值/的themes.xml

    <style name="AppTheme" parent="Theme_one">
    </style>
    <style name="Theme_one" parent="@style/Theme.AppCompat.Light">
        <item name="windowActionBar">false</item> 
        <item name="foo">@style/foo_style</item>
        <!-- bunch of other styles -->
    </style>

RES /值/ Styles.xml

<style name="foo_style" parent="@style/Theme.AppCompat.Light">
    <item name="android:background">@color/actionBarDark</item>
</style>

RES /值-V21 /的themes.xml

<style name="AppTheme" parent="Theme_one">
    <!-- some v21 specific items not related to toolbar like animations etc-->
</style>

RES /值-V21 / Styles.xml

<style name="foo_style" parent="@style/Theme.AppCompat.Light">
    <item name="android:colorPrimaryDark">@color/actionBarDark</item> <!-- THIS DOES NOT WORK !!! -->
    <item name="android:background">@color/actionBarDark</item>
    <item name="android:elevation">5dp</item>
</style>

此安排适用于Android API v11至v19。 我对v21的问题(我也尝试了2和3没有android:前缀,结果相同):

1) android:background 工作正常! 2) android:colorPrimaryDark 不起作用!
3) android:elevation 适用于ToolbarToolbar按钮的标题,如下所示。这是预期的吗?
enter image description here

我错过了什么?显然,我没有以正确的方式进行样式设置,但无法找到任何谈论toolbar样式的资源!

1 个答案:

答案 0 :(得分:1)

通过移动:

,我得到了 colorPrimaryDark
<item name="colorPrimaryDark">@color/actionBarDark</item>

res / values-v21 / Styles.xml res / values-v21 / Themes.xml

我通过将提升从所有样式或主题xmls中删除,并将其放入android.support.v7.widget.Toolbar声明中来实现提升

此外,如果我在此处定义另一个主题并使用Content.setTheme()在应用程序中动态更改主题,则主题会更改,但状态栏颜色不会更改。欢迎任何建议。