Android,如何仅为TextView小部件设置主题字体颜色? [英] Android, How to theme font colour for just TextView Widgets?

查看:263
本文介绍了Android,如何仅为TextView小部件设置主题字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用

<style name="AppBaseTheme" parent="Theme.Sherlock.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/background</item>
</style>

现在我希望将文本视图的颜色更改为白色,以便在深色背景下可以看到它们

and now I wish to change the colour of just my text views to white so that they are visible against the dark background

我认为为TextView设置android:textColor会达到这样的效果

I thought that setting the android:textColor for TextView would do the trick like so

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:textViewStyle">@style/Ff1TextViewStyle</item>
</style>


<style name="Ff1SpinnerViewStyle" parent="android:Widget.TextView">
    <item name="android:textColor">@color/sysWhite</item>
    <item name="android:textStyle">bold</item>
</style>

但令我沮丧的是,这完全塞满了各种内容,例如微调框文本和微调框下拉菜单文本,ActionBar下拉菜单,方法是将文本更改为白色背景上的白色,所有这些我之前都很满意,而我确保我尚未使用或尚未发现的其他控件最终会以白色文本结尾.

But to my dismay this just totally stuffs up all sorts of things like spinner text and spinner dropdown text, ActionBar Drop Down menus by changing the text to white on white backgrounds all of which I was perfectly happy with before plus I'm sure other controls that I haven't used yet or haven't discovered yet could well end up with white text.

我很难相信android主题是如此混乱,以至于我无法自信地设置TextView widgtes的文本颜色而不会影响那些我只能假设是TextView小部件的子部件的小部件,所以我在思考只能归咎于我缺乏知识,我希望并正在寻求启发,因为这必须是一个共同的要求.

I find it hard to believe that android themes are so messed up that I am unable to confidently just set the text colour of TextView widgtes without affecting widgets that, what I can only assume, are children of TextView widgets so I am thinking that it must just be my lack of knowledge that is to blame and I am hoping and looking for enlightenment for what must be a common requirement.

任何帮助表示赞赏.

推荐答案

我不太确定是否只能为TextView设置样式,因为ActionbarSpinner和其他Views包含TextView也是他们的孩子,所以您发现它也会影响他们.我认为您应该创建一种样式并将其设置为您的TextView.与此类似:

I'm not really sure if you can set a style only for TextView, because Actionbar , Spinner and other Views contains TextView as their childs too, so it will affect them too as you find out. In my opinion you should create a style and set it to your TextView's. Something similar to this :

 <style name="MyTextView" parent="@android:style/TextAppearance.Small">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">12sp</item>
</style>

并在TextView的xml声明中使用它:

and use it in your TextView's xml declaration :

<TextView
   style="@style/MyTextView" />

或您可以做的第二件事是创建自定义TextView

or the second thing which you can do is to create your custom TextView

public class MyTextView extends TextView {

     public MyTextView(Context context){
         super(context);
         this.setTextColor(android.R.color.white);
     }
}

,并在您的xml文件中使用MyTextView:

and use MyTextView in your xml files :

<com.my.package.name.MyTextView
   android:id="@+id/my_textview" />

设置android:textViewStyle会影响所有其他视图的原因仅仅是因为如果您在android源代码中仔细查看,将会看到其他视图中使用的每个textview样式都是android.Widget.TextView的子级.因此,在我看来,在这种情况下,您可以做的最好的事情就是创建一个样式并将其设置为xml中的TextView.

The thing why setting android:textViewStyle affects all other view's is just because if you look closer in android source you will see that every textview style used in other views is child of android.Widget.TextView . So the best thing which you can do in this situation in my opinion is to create a single style and set it to your TextView 's in xml.

希望这些信息会有所帮助!

Hope this information will help!

这篇关于Android,如何仅为TextView小部件设置主题字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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