在Android的浏览设置全局样式 [英] Setting global styles for Views in Android

查看:104
本文介绍了在Android的浏览设置全局样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我想在我的应用程序的所有TextView的实例有文字颜色=#FFFFFF。有没有一种方法来设置它的设置为每个TextView中的,在一个地方呢?

Let's say I want all the TextView instances in my app to have textColor="#ffffff". Is there a way to set that in one place instead of setting it for each TextView?

推荐答案

其实,你可以设置为TextViews(以及大多数其他内置控件)缺省的风格,而不需要做一个自定义的Java类或单独设置样式。

Actually, you can set a default style for TextViews (and most other built-in widgets) without needing to do a custom java class or setting the style individually.

如果你在<一看href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml"><$c$c>themes.xml在Android源代码,你会看到一堆的各种部件的默认样式属性。最关键的是,你在你的自定义主题覆盖 textViewStyle (或 editTextStyle 等)的属性。您可以通过以下方式来覆盖这些:

If you take a look in themes.xml in the Android source, you will see a bunch of attributes for the default style for various widgets. The key is the textViewStyle (or editTextStyle, etc.) attribute which you override in your custom theme. You can override these in the following way:

创建一个 styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
    <item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:textColor">#F00</item>
    <item name="android:textStyle">bold</item>
</style>
</resources>

然后,只需应用该主题到应用程序中的Andr​​oidManifest.xml

<application […] android:theme="@style/MyTheme">…

和你所有的文本视图将默认为在 MyTextViewStyle 中定义的样式(在这种情况下,大胆的和红色)!

And all your text views will default to the style defined in MyTextViewStyle (in this instance, bold and red)!

这是由API级别4起测试的设备,并似乎工作太棒了。

This was tested on devices from API level 4 onward and seems to work great.

这篇关于在Android的浏览设置全局样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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