zoukankan      html  css  js  c++  java
  • Android 修改 TextView 的全局默认颜色。

    如果你的应用中大多数TextView的颜色是红色, 或者其他颜色, 你是为每一个TextView都设置一次颜色, 还是有其他更好的办法, 这里教你怎么修改TextView的默认颜色。

    当然我们TextView的远吗入手。

    通过查看 TextView 源码, 发现如下代码:
    public TextView(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.textViewStyle);
    }

    这段代码表示, TextView的默认样式名称是 textViewStyle。 然后在Themes.xml中的Theme下面找到 textViewStyle, 可以发现如下代码
    <item name="textViewStyle">@android:style/Widget.TextView</item>
    而且不同的主题TextViewStyle的值是不一样的。其中:
    • Theme 下面是 @android:style/Widget.TextView;
    • Theme.Height 下面没有这个值, 不知道为什么;
    • Theme.Holo 下面是 :@android:style/Widget.Holo.TextView;
    • Theme.Holo.Light 下面是 @android:style/Widget.Holo.Light.TextView;
     
    接着查看源码TextView, 在509行发现如下代码:
    case com.android.internal.R.styleable.TextAppearance_textColor:
                        textColor = appearance.getColorStateList(attr);
                        break;
    这段代码的功能就是用于获取颜色的。 可以发现是通过com.android.internal.R.styleable.TextAppearance_textColor的值。 然后我们查看style.xml文件, 找到如下代码:
        <style name="Widget.TextView">
            <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
            ...
        </style>
    Widget.TextView 就是上文找的 Theme下面的TextViewStyle的值。 然后我们看到 android:textAppearance 这个就是TextView源码中提到过的。 自然接下来查看:?android:attr/textAppearanceSmall 在Theme中定义的值的是什么。
    <item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
    我们看到textAppearanceSmall值是 @android:style/TextAppearance.Small, 然后当然要找到@android:style/TextAppearance.Small
    在style.xml中找到:
        <style name="TextAppearance.Small">
            <item name="android:textSize">14sp</item>
            <item name="android:textColor">?textColorSecondary</item>
        </style>
     
    可以看到颜色的定义名称是 ?textColorSecondary, 到这里我们终于找到定义颜色的地方了。 这个各个主题鲜明都有定义,不止一处。
    <item name="textColorSecondary">@android:color/secondary_text_dark</item>
     
    在Theme中我们终于看到定义TextView的颜色的代码了。 比如把TextView默认颜色改为 #333333, 使用如下代码
        <style name="AppTheme" parent="Theme">
            <item name="android:textColorSecondary">#333333</item>
        </style>
  • 相关阅读:
    图片处理
    RBAC打造通用web管理权限
    决定人生的三种成本:机会成本,沉没成本,边际成本
    实验楼可以做各种程序实验
    .net2.0 C# Json反序列化
    不装mono,你的.NET程序照样可以在Linux上运行!
    .net framework4与其client profile版本的区别
    spark transform系列__sortByKey
    自己主动化开发測试的一些理论根据及经验总结(2015)
    Toast分析——实现自己的Toast
  • 原文地址:https://www.cnblogs.com/yuxing/p/3312668.html
Copyright © 2011-2022 走看看