简体   繁体   English

保持与材料设计的向后兼容性

[英]Maintaining backward compatibility with Material Design

I'm trying to implement an activity that has the Material Design tinted title bar. 我正在尝试实现一个带有Material Design着色标题栏的活动。

My standard style is: 我的标准样式是:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>

My v21 style is: 我的v21风格是:

<style name="AppTheme" parent=" -see rest of this post- ">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:colorAccent">@color/accent</item>
</style>

and the results I get are: 我得到的结果是:

API 18:
MyActivity extends AppCompatActivity
-- Black title bar, this is good enough.
MyActivity extends Activity:
-- No title bar.
API 21:
MyActivity extends Activity, parent="android:Theme.Material.Light"
-- Perfect green tinting of status bar and title bar.
MyActivity extends AppCompatActivity, parent="android:Theme.Material.Light"
-- Crashes with: You need to use a Theme.AppCompat theme (or descendant) with this activity.
MyActivity extends AppCompatActivity, parent="Theme.AppCompat.Light"
-- Status bar is correctly green tinted. Title bar has no background colour.
MyActivity extends AppCompatActivity, parent="Theme.AppCompat.Light.DarkActionBar"
-- Status bar is correctly green tinted. Title bar has black background colour.

How do I get the coloured title bar in Lollipop and an acceptable one pre-Lollipop? 如何获得棒棒糖中的彩色标题栏和可接受的一个棒棒糖之前的标题? I know with extra work I can have a coloured pre-lollipop title bar, but that is not needed at this time. 我知道通过额外的工作,我可以有一个彩色的棒棒糖前标题栏,但是目前不需要。

You should be using the non-android namespaced properties with the support library: 您应该在支持库中使用非Android命名空间的属性:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

No v21 version required. 不需要v21版本。 This will give you consistent behavior back to API level 7 这将使您的行为恢复到API级别7

If you are using AppCompat, then all of the material color palette attributes (such as colorPrimary ) are available to all API levels, so you can write a single theme: 如果您使用的是AppCompat,则所有材料调色板属性(例如colorPrimary )都可用于所有API级别,因此您可以编写一个主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

And use AppCompatActivity for all of your activities as per the Consistent Design with AppCompat DevByte . 并使用AppCompatActivity您的所有活动的具体根据使用程序兼容性DevByte一贯的设计 This will give you an action bar with colorPrimary on all API7+ devices, a status bar of colorPrimaryDark on API21+ devices (older devices do not support colored status bars), and light text on your action bar (use Theme.AppCompat.Light if you want dark text on your action bar). 这将在所有API7 +设备上为您提供带有colorPrimary的操作栏,在API21 +设备上为colorPrimaryDark提供状态栏(较旧的设备不支持彩色状态栏),并在操作栏中提供浅色文本(如果需要,请使用Theme.AppCompat.Light操作栏上的深色文字)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM