Android 将样式应用于自定义 TextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3452071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android apply style to custom TextView
提问by Ian Vink
I have a class, TextViewStyled, which extends TextView
我有一个类TextViewStyled,它扩展了 TextView
In my theme XML, how do I apply a style to all my TextViewStyledwidgets on Activities with a chosen theme?
在我的主题 XML 中,如何将样式应用到具有所选主题的活动上的所有TextViewStyled小部件?
I have this simple theme, but I want to limit the Black Gold style to TextViewStyledWidgets without specifying the Black Gold in the TextViewStyled style attribute. This is one of many themes which will be switched dynamically.:
我有这个简单的主题,但我想将 Black Gold 样式限制为TextViewStyled小部件,而不在 TextViewStyled 样式属性中指定 Black Gold。这是将动态切换的众多主题之一。:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyThemeOneofMany" parent="android:Theme.NoTitleBar.Fullscreen">
<item name="android:???????????">@style/TextViewStyled_Black_Gold</item>
</style>
<style name="TextViewStyled_Black_Gold" parent="@android:style/Widget.TextView">
<item name="android:background">#1E1921</item>
<item name="android:textColor">#A85E4F</item>
<item name="android:textColorLink">#FFBC4F</item>
<item name="android:textStyle">normal</item>
</style>
</resources>
采纳答案by Ian Vink
I found that updating the styles works when overriding the base textview
class after the inflation
event.
我发现textview
在inflation
事件发生后覆盖基类时更新样式有效。
回答by Gennadiy Ryabkin
For AppCompattheme (2.3.3+):
对于AppCompat主题 (2.3.3+):
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textViewStyle">@style/PizzaSizeWidget</item>
</style>
<style name="CustomTextView" parent="@android:style/TextAppearance.Widget.TextView">
<item name="android:background">#1E1921</item>
<item name="android:textColor">#A85E4F</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">93sp</item>
</style>
回答by AgentKnopf
In order to set the style for TextViews in your custom Theme use the name "android:textViewStyle":
为了在您的自定义主题中设置 TextViews 的样式,请使用名称“android:textViewStyle”:
<style name="MyThemeOneofMany" parent="android:Theme.NoTitleBar.Fullscreen">
<item name="android:textViewStyle">@style/TextViewStyled_Black_Gold</item>
</style>
Found it when looking through the official android themes.xml[click].
在浏览官方android themes.xml [点击]时找到了它。