如何在android中运行时设置主题

时间:2014-09-17 13:20:56

标签: android android-actionbar android-theme

我试图在运行时设置主题,除了动作栏一切正常。

我在super.oncreate(...)

之前在oncreate()中设置主题

如何在操作栏中设置主题?

这是我在style.xml中的主题

<style name="abcThemeTest" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@color/RED</item>
    <item name="actionBarStyle">@style/MyActionBarTest</item>
</style>


<style name="MyActionBarTest" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">@color/RED</item>        
       <item name="android:background">@color/RED</item>
    <item name="titleTextStyle">@style/MyActionBarTitleTextTest</item>
</style>

 <style name="MyActionBarTitleTextTest" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/WHITE</item>
    <item name="android:textStyle">bold</item>

</style>

1 个答案:

答案 0 :(得分:0)

只需像这样修改您的样式文件。

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="android:actionMenuTextColor">@color/actionbar_text</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="actionMenuTextColor">@color/actionbar_text</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

        <!-- Support library compatibility -->
        <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
           parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>

    <!-- ActionBar tabs text -->
    <style name="MyActionBarTabText"
           parent="@style/Widget.AppCompat.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>
</resources>