【问题标题】:How to set android application custom theme with custom action bar background?如何使用自定义操作栏背景设置 android 应用程序自定义主题?
【发布时间】:2023-04-02 06:45:01
【问题描述】:

我想为我的 android 应用程序设置自定义主题,但无法这样做。以下是我的 xml sn-p :

res/values/themes.xml

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

<resources>

    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">#FF0000</item>
    </style>

</resources>

在清单中,我已将 android:theme 属性更改为 :
android:theme="@style/CustomActionBarTheme"

但是,它仍然不适合我。我得到的主题是 Theme.AppCompat.Light.DarkActionBar。

【问题讨论】:

    标签: android android-layout android-manifest android-theme android-styles


    【解决方案1】:

    我通过修改style.xml找到了解决办法:

    <!-- Base application theme. -->
    <style name="CustomAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#FF0000</item>
    </style>
    

    【讨论】:

    • 那是因为,当您使用AppCompat 时,您定义的属性没有android: 前缀,即。 android:backgroundandroid:colorPrimary。如果您想让您的应用向后兼容,请仔细阅读此页面:Styling the Action Bar
    【解决方案2】:

    您需要创建 res/values/style.xml 或在 res/values/style.xml 中进行这些更改

    themes.xml 在 android 项目中不起作用。

    并在清单中设置您的自定义主题

    <application ...
    android:theme="@style/Your_Custom_Theme_Name"
    >
     ...
    </application>
    

    【讨论】:

    • 但是我在 developer.android.com 上找到了这种方式。另外,你提到的那个不工作
    • 是的。我正在扩展它。
    【解决方案3】:

    res/values/style.xml 中添加您的CustomActionBarTheme 主题,它会起作用。

    【讨论】:

    • 但是我在 developer.android.com 上找到了这种方式。另外,你提到的那个不工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多