如何避免向后兼容android应用程序中的样式重复

如何避免向后兼容android应用程序中的样式重复,android,android-appcompat,Android,Android Appcompat,我有一个基于appcompat兼容性库构建的应用程序。我在values/styles.xml中为我的应用程序定制了一些样式,如按钮、UI等。我有values-11/styles.xml values-16/stylex.xml values-21/styles.xml 我的问题是:我不想在每个styles.xml文件中重复自定义样式代码。我希望较新的版本继承自main syles.xml,只编写新的样式,但此方法不起作用,并且我收到错误,说我没有为我的活动设置主题 以下是我的风格: values

我有一个基于appcompat兼容性库构建的应用程序。我在values/styles.xml中为我的应用程序定制了一些样式,如按钮、UI等。我有values-11/styles.xml values-16/stylex.xml values-21/styles.xml

我的问题是:我不想在每个styles.xml文件中重复自定义样式代码。我希望较新的版本继承自main syles.xml,只编写新的样式,但此方法不起作用,并且我收到错误,说我没有为我的活动设置主题

以下是我的风格: values/styles.xml:

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>
<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

@可绘制/集成电路菜单添加用户全息灯

values-v11/styles.xml:

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>
<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

在我的andoirdmanifest文件中,我已将我的活动主题引用到

Holo.Demo.Theme.Light.DarkActionBar


在Eclipse中创建新项目时,将在values/styles.xml中生成两种样式:

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>
<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

阅读评论,它们很有用。首先,如果您仍在使用AppCompat,请始终使用它。否则:第二,您可能会在“全局”AppTheme中为样式按钮或textview编写一些行,假设它们都存在于所有版本/API中。如果您想更改API 14中引入的smth,则在Eclipse中创建新项目时,将在values/styles.xml中生成两种样式,即在v14样式中编写适当的行AppBASETheme

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>
<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

阅读评论,它们很有用。首先,如果您仍在使用AppCompat,请始终使用它。否则:第二,您可能会在“全局”AppTheme中为样式按钮或textview编写一些行,假设它们都存在于所有版本/API中。如果您想更改API 14中引入的smth,则在Eclipse中创建新项目时,将在values/styles.xml中生成两种样式,即在v14样式中编写适当的行AppBASETheme

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>
<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

阅读评论,它们很有用。首先,如果您仍在使用AppCompat,请始终使用它。否则:第二,您可能会在“全局”AppTheme中为样式按钮或textview编写一些行,假设它们都存在于所有版本/API中。如果您想更改API 14中引入的smth,则在Eclipse中创建新项目时,将在values/styles.xml中生成两种样式,即在v14样式中编写适当的行AppBASETheme

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>
<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
阅读评论,它们很有用。首先,如果您仍在使用AppCompat,请始终使用它。否则:第二,您可能会在“全局”AppTheme中为样式按钮或textview编写一些行,假设它们都存在于所有版本/API中。如果您想更改API 14中引入的smth,只需在v14样式的AppBASETheme中编写适当的行即可