关于TextView的坑集合
2017-07-15 17:24
232 查看
1.关于默认字体颜色
项目中出现莫名其妙的字体不显示,实际是因为没有设置默认的字体颜色。对比检查代码没有发现style中有设置文字颜色的默认值。
通过查看 TextView 源码, 发现如下代码:
这段代码表示, TextView的默认样式名称是 textViewStyle。 然后在Themes.xml中的Theme下面找到 textViewStyle, 可以发现如下代码
而且不同的主题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行发现如下代码:
这段代码的功能就是用于获取颜色的。 可以发现是通过com.android.internal.R.styleable.TextAppearance_textColor的值。 然后我们查看style.xml文件, 找到如下代码:
Widget.TextView 就是上文找的 Theme下面的TextViewStyle的值。 然后我们看到 android:textAppearance 这个就是TextView源码中提到过的。 自然接下来查看:?android:attr/textAppearanceSmall 在Theme中定义的值的是什么。
我们看到textAppearanceSmall值是 @android:style/TextAppearance.Small, 然后当然要找到@android:style/TextAppearance.Small
在style.xml中找到:
可以看到颜色的定义名称是 ?textColorSecondary, 到这里我们终于找到定义颜色的地方了。 这个各个主题鲜明都有定义,不止一处。
在Theme中我们终于看到定义TextView的颜色的代码了。 比如把TextView默认颜色改为 #333333, 使用如下代码
暂时的解决方法是记住即使是黑色的也设置颜色值。
项目中出现莫名其妙的字体不显示,实际是因为没有设置默认的字体颜色。对比检查代码没有发现style中有设置文字颜色的默认值。
通过查看 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>
暂时的解决方法是记住即使是黑色的也设置颜色值。
相关文章推荐
- 关于TextView分段截取并响应点击(或者将字符串集合拼接在在一个Textview中并分段响应)
- 关于新闻头条上面TextView下面ViewPager第三方应用
- Android 关于TextView的操作
- 关于在listview中加textview的autolink的问题
- 关于textview显示省略号和设置最大行数的问题
- 关于TextView文字下划线
- 关于textView,textField及delegate的使用
- 关于TextView和EditText字间距的问题
- 【android学习】关于textview.setEllipsize(TextUtils.TruncateAt.valueOf("END"));失效
- 关于TextView的setText()方法报resource not found exception的问题
- 关于带图片的TextView-代码更改图片位置注意事项
- 关于动态更新 安卓 TExtview的 代码
- 关于AutoCompleteTextView中调用setOnItemClickListener中参数调用介绍
- 关于textView上显示placeholder
- 关于 This tag and its children can be replaced by one <TextView/> and a compound drawable 的问题
- 关于Android中textView的背景设置
- 关于TextView中修改部分字体颜色、大小方法的总结
- 关于TextView文字下划线
- 关于限制TextView的输入问题
- 转:关于android webview实践的文章集合