【发布时间】:2014-11-16 14:45:31
【问题描述】:
我一直在关注关于 Android 开发人员培训的 myfirstapp 指南,但我遇到了一个问题,they do not properly explain how to define colors.
他们提到要创建自定义主题,您可以这样声明文本颜色:
主题.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:textColor">@style/MyActionBarTitleText</item>
<item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/actionbar_text</item>
</style>
<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
parent="@android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textColor">@color/actionbar_text</item>
</style>
</resources>
他们没有提到如何指定@color/actionbar_text,但常识(和一些谷歌搜索)表明 values 包中需要一个 colors.xml 文件:
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="actionbar_text">#ff355689</color>
</resources>
但是,当尝试运行应用程序时,它会报错:
Process: com.example.myfirstapp, PID: 25997
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
如果我从 colors.xml 中删除该行,它会在找不到颜色参考时出错。我比较确定我的代码是正确的,任何人都可以看到任何错误吗?
编辑
我只是想指出,我实际上对themes.xml 文件使用了稍微不同的语法,因为教程中的那些不编译。本教程使用@style/Widget.Holo.ActionBar.TabText,我发现它实际上是android的一个属性,所以我需要使用@android:style/Widget.Holo.ActionBar.TabText来代替。
【问题讨论】:
标签: java android xml android-styles