为所有 TextView(或自定义视图)设置样式,而不向每个 TextView 添加样式属性 [英] Styling all TextViews(or custom view) without adding the style attribute to every TextView

查看:19
本文介绍了为所有 TextView(或自定义视图)设置样式,而不向每个 TextView 添加样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法格式化所有的 TextViews、Buttons 或任何带有主题的东西?当我想格式化所有 a-tags 时,就像在 CSS 中一样

Is there a way to format all TextViews, Buttons or whatever with a theme ? Like in CSS when i want to format all a-tags

一个{/这里有一些 CSS/}

当我将主题应用到我的应用程序时,我想通过 xml 在 android 中执行此操作.有什么想法吗?谢谢

I want to do that in android via xml when I'm applying a theme to my application. Any ideas ?Thanks

http://bartinger.at/

更新 1.0:我想创建一个主题,将所有 TextViews 绿色和所有 EditTexts 红色的文本格式化.这样我只需应用主题,就不必担心样式属性!

Update 1.0: I want to create a theme that formats the text in all TextViews green and in all EditTexts red. So that i just apply the theme and I never have to worry about the style attribute!

更新 1.1:所以我找到了那段代码,我认为这是一个好的开始

Update 1.1: So I found some that piece of code and I think that's a good beginning

<item name="android:textViewStyle">@style/MyTextView</item>
<item name="android:buttonStyle">@style/MyButton</item>

我认为这就是我的问题的答案.但我还有一个.我想编写自己的 ActionBar 并想知道如何应用默认样式或默认属性(再次在布局 xml 中添加样式属性:P)

I think thats the answer to my question. But I have another one. I want to write my own ActionBar and wanted to know how I can apply a default style or default attributes (again without adding the style attribute in the layout xml :P )

我有课

public class ActionBar extends LinearLayout{ }

我会在我的应用程序中这样使用它

and I'm gonna use it like that in my application

<at.bartinger.uil.ActionBar>....</at.bartinger.uil.ActionBar>

ActionBar 应该有一些默认属性(如高度和宽度),然后添加一些自定义样式属性,这些属性可能会因应用程序而异(如背景)

The ActionBar should have some default attributes (like height and width) and then adding some custom style attributes which could change from app to app (like background)

推荐答案

是的,您可以将主题应用于整个应用程序,然后您的所有文本视图都将具有该样式.

yes you can you can apply a theme to the whole application and then all your textviews will have that style.

您必须在 styles.xml 文件中定义您的 CustomTheme

Inside the styles.xml file you have to define your CustomTheme

例如:

<style name="CustomTheme" parent="android:Theme.Light">
  <item name="android:windowBackground">@color/custom_theme_color</item>
  <item name="android:colorBackground">@color/custom_theme_color</item>
</style>

您添加类似文本的内容"android:textStyle="myStyle" 并在 Mystyle 中指定详细信息

you add something like text "android:textStyle="myStyle" and specify the details in Mystyle

这篇关于为所有 TextView(或自定义视图)设置样式,而不向每个 TextView 添加样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆