Android样式,API级别之间的区别

4

我正在尝试学习如何使用styles.xml文件为应用程序设置样式,其中有一些问题需要澄清才能理解。

  1. 在一个Item中,设置android:actionbarstyle和只设置actionbarstyle有什么区别?我知道在这种情况下,我必须同时定义两者,但是为什么?对于所有其他情况呢?例如android:colorPrimary和只有colorPrimary?在那种情况下,我会收到一个错误,说android:colorPrimary只能与min API级别21一起使用。那么,有人能够对android:前缀的作用进行解释,以及它如何影响我的应用程序吗?

  2. 是否有一个不同父样式的参考,例如parent="@style/Widget.AppCompat.Light.ActionBar以及它们的含义?如何找到特定项目可用的不同父样式列表,以及我可以在其中“override”什么?目前,这主要是我猜测....

仅作参考,以下是我的当前styles.xml文件。

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="MyTheme"/>

    <style name="MyTheme" parent="Theme.AppCompat.Light">
        <item name="actionBarTheme">@style/MyTheme.ActionBarTheme</item>
        <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
        <item name="actionBarStyle">@style/MyTheme.ActionBarStyle</item>

        <item name="colorPrimary">@color/my_green</item>
        <item name="colorPrimaryDark">@color/my_forest</item>
        <item name="colorAccent">@color/my_soil</item>

        <item name="drawerArrowStyle">@style/MyTheme.DrawerArrowStyle</item>

        <item name="android:actionOverflowButtonStyle">@style/MyTheme.OverFlow</item>

        <item name="android:actionMenuTextColor">@color/white</item>

        <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
        <item name="android:homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>

        <item name="colorControlNormal">@color/my_green</item>
        <item name="colorControlActivated">@color/my_forest</item>
        <item name="colorControlHighlight">@color/my_deep_green</item>
    </style>

    <style name="MyTheme.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
        <!-- This sets the BACK arrow to white. Otherwise it's black. Must be placed in the theme-->
        <item name="colorControlNormal">@color/white</item>
    </style>

    <style name="MyTheme.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@color/my_green</item>
        <item name="background">@color/my_green</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="colorControlNormal">@color/white</item>
    </style>

    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>

    <style name="MyTheme.DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@color/white</item>
    </style>

    <style name="MyTheme.OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
        <item name="android:tint">@color/white</item>
    </style>
</resources>
1个回答

3

我会尽力解释,并集中讲解以下内容:

<item name="colorPrimary">@color/my_green</item>
<item name="colorPrimaryDark">@color/my_forest</item>
<item name="colorAccent">@color/my_soil</item>

这些属性通常在API 21及以上版本中可用。通常,您使用带有“android”前缀的属性。
如果您将所有样式定义在values文件夹下的styles.xml中,并且正在使用app compat,则需要同时使用两个前缀。
如果没有前缀,则该属性适用于L以前的设备,即App Compat。要使其适用于L及更高版本的设备,您需要再次使用“android”前缀来指定属性。
要获取其他Android样式,您可以像进入类和实现一样进入它们。对于Mac,我按下命令键,然后用鼠标单击特定样式。

好的,这是一个很好的核心解释。创建一个values-v21文件夹和其中的styles.xml文件,将所有没有android:标签的属性和其他属性放在常规的styles.xml中,这是一个好主意吗? - Joakim
在values-v21文件夹中,你需要使用带有前缀的方式,而在标准文件夹中则不需要(如果与App Compat相关)。在values文件夹中使用"colorPrimary",而在values-v21文件夹中使用"android:colorPrimary"。但只有在需要覆盖某些属性时,我才会在values-v21中放置特殊内容。 - Thomas R.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接