首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将包含textColor的样式应用于textView的textAppearance时,文本的颜色不会更改

将包含textColor的样式应用于textView的textAppearance时,文本的颜色不会更改
EN

Stack Overflow用户
提问于 2017-07-10 17:03:56
回答 3查看 2.3K关注 0票数 6

我想减少xml代码的重复。所以我用textView做了一些标准的文本样式。我们可以在textView的'style‘属性下应用样式,也可以在'android:textAppearance’属性下应用样式。

下面是我为文本外观设计的一些样式--

代码语言:javascript
运行
复制
<style name="Grey">
    <item name="android:textColor"> #333333 </item>
</style>

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor"> #00FF00 </item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">20sp</item>
</style>

当我在'textAppearance‘属性下应用这些样式时,上述样式中的文本颜色都不会改变。它在textView的'style‘属性下工作。

代码语言:javascript
运行
复制
//textColor not working
<TextView
    android:id="@+id/profile_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Full Name"
    android:textAppearance="@style/CodeFont"/>

//textColor working
<TextView
    android:id="@+id/profile_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Full Name"
    style="@style/CodeFont"/>

我希望它们在“textAppearance”属性下工作,这样我就可以在“style”属性下应用一些其他样式。根据android documentation,我们可以在'textAppearance‘属性下应用textColor样式。

请对此提出一些解决方案。谢谢

EN

回答 3

Stack Overflow用户

发布于 2017-07-10 17:14:59

尝试将小部件中的文本颜色设置为空,如下所示:

代码语言:javascript
运行
复制
<TextView
android:id="@+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="@null"  //add this line
android:textAppearance="@style/CodeFont"/>

另外,我认为你应该尝试无效缓存并重启Android Studio。导入和链接问题有时可以像这样解决。

票数 5
EN

Stack Overflow用户

发布于 2017-07-10 17:20:15

此代码片段适用于我

代码语言:javascript
运行
复制
 <style name="fonts" parent="TextAppearance.AppCompat">
    <item name="android:textColor">#245546</item>
    <item name="android:textSize">30dp</item>
</style>

而textview是

代码语言:javascript
运行
复制
  <TextView
    android:id="@+id/sample"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to stackoverflow"
    android:textAppearance="@style/fonts"/>
票数 2
EN

Stack Overflow用户

发布于 2020-12-15 16:15:29

有时如果你不给android:textColor in样式,你可能会遇到这个问题,文本颜色不会出现在你的TextView中,所以解决方案是完全给出<item name="android:textColor">@color/yourColor</item> in样式,而不是这个<item name="textColor">@color/yourColor</item>。您的问题将得到解决:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45007858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档