在多个文本视图中设置文本颜色

3

我想要改变很多textView的文本颜色,并将其设置为红色。 现在,这是我的xml代码,第一个textView已经被改成了红色:

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="150dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/data1_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Latitude" 
            android:textColor="#FF0000"/>

        <TextView
            android:id="@+id/data1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Latitude_val" />

        <TextView
            android:id="@+id/data2_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Longitude" />

        <TextView
            android:id="@+id/data2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Longitude_val" />

        <TextView
            android:id="@+id/data3_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Altitude" />

        <TextView
            android:id="@+id/data3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Altitude_val" />

        <TextView
            android:id="@+id/data4_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Vitesse GPS" />

        <TextView
            android:id="@+id/data4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Vitesse GPS_val" />
    </LinearLayout>

如你所见,这些textViews位于一个线性布局内,但遗憾的是,在布局参数中不能直接使用android:textColor

这里我只展示了4个textViews,但实际上还有很多,我不想逐个赋值给它们(想象一下如果我之后想要改变颜色的话……)

是否有某种标签可以为一组textViews指定样式?


1
拥有一个自定义的TextView并为其设置颜色。您可以在应用程序中使用它。http://developer.android.com/guide/topics/ui/themes.html - Raghunandan
请使用style属性,并在您的样式中定义textColor。 - Houcine
@WhiskThimble 你也可以使用strings.xml来存储颜色值。 - Sankar V
我确实可以使用自定义样式,这对我在需要更改颜色时很有帮助,但是我仍然必须在每个textView中声明属性“style”,这可能会非常冗长,与其有用性相比。 - WhiskThimble
2个回答

4

以下内容适用于您。



    private void setTextColor()
    {
    /*put id(s) of all the TextView you want to change the color of,
    In an array and call setTextColor() for all the array items*/
    int textViews[]={R.id.data1_text,R.id.data1,R.id.data2_text,R.id.data2,R.id.data3_text,R.id.data3};
    for(int i=0;i<textViews.length;i++)
    ((TextView)findViewById(textViews[i])).setTextColor(Color.RED);
    }


现在,在你的onCreate()方法中调用setTextColor()。你也可以创建类似的函数来设置文本大小和其他好玩的东西。

0

您可以通过编程方式为编辑文本添加颜色,例如:

EditText et = (EditText) findViewById(R.id.edit1);
// to set text color using RGB code
et.setTextColor(Color.parseColor("#FF0000"));

你也可以用XML来做,比如:

android:textColor="#FF0000"

最好的方法是先在/res/values/mycolor.xml中创建颜色的xml定义,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>

并设置EditText的颜色,如:

 android:textColor="@android:color/white"

并且以编程方式实现,例如:

 et.setTextColor(R.color.white);

希望它有所帮助;


这不完全是我要问的。你说的一定是正确的,但你必须逐个为每个textView做,而这正是我想避免的事情。 - WhiskThimble

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接