【问题标题】:How to change the color theme of android app?如何更改android应用程序的颜色主题?
【发布时间】:2016-11-01 22:49:15
【问题描述】:

我一直按照说明如何更改color,但程序产生了这个错误:

06-29 19:20:39.416 7041-7041/com.example.lucerne.adapter_example_2 E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.lucerne.adapter_example_2, PID: 7041
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lucerne.adapter_example_2/com.example.lucerne.adapter_example_2.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

目前,style.xml 如下所示:

<!--<color name="white_opaque">#FFFFFFFF</color>-->
<!--<color name="pitch_black">#FF000000</color>-->

<!--<style name="AppTheme" parent="android:Theme.Light">-->
    <!--<item name="android:background">@color/white_opaque</item>-->
    <!--<item name="android:windowBackground">@color/white_opaque</item>-->
    <!--<item name="android:colorBackground">@color/white_opaque</item>-->

<!--</style>-->

 <!--Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!--<style name="AppTheme" parent="@android:style/Theme.Holo.Light">-->
    <!--<item name="android:actionBarStyle">@style/MyActionBarTheme</item>-->
<!--</style>-->
<!--<style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">-->
    <!--<item name="android:background">#FF0000</item>-->
<!--</style>-->

是什么导致了错误,以及如何将颜色更改为其他颜色?

【问题讨论】:

    标签: android xml colors background-color


    【解决方案1】:

    正如错误所说,您必须以您的风格扩展 Theme.AppCompat。

    这样的简单例子:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
      </style>
    
     <style name="Theme.SemiTransparent" parent="AppTheme.NoActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/colorSemiTransparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
      </style>
    

    查看第一个样式的样式名称,AppTheme.NoActionBar,它使用 AppTheme 作为其基础。然后在第二种样式中,AppTheme.NoActionBar,它使用以 NoActionBar 作为其父级的 AppTheme。

    使用 AppTheme 作为基础后,您可以使用自定义样式更改视图的颜色。

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多