我编程方式创建一个列表(没有一个ListView,只是将其添加到父)这样的元素:
I programmatically create a list (no a ListView, just adding them to the parent) of such elements:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_weight="1">
<TextView android:id="@+id/filiale_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/lagerstand_text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="10sp" android:textColor="@color/red"/>
</LinearLayout>
另外,我已经在价值观/ colors.xml定义一些颜色。正如你看到的,TextView的ID为lagerstand_text已设置它的颜色为红色默认。这一工程。
Also, I have defined some colors in values/colors.xml. As you see, the TextView with id "lagerstand_text" has set it's color to red by default. That works.
在创建Java中的元素,我
When creating the elements in Java, I do
lagerstandText.setText("bla");
和一些元素也是我做的。
and for some elements also I do
lagerstandText.setTextColor(R.color.red);
和其他颜色。虽然关于这一点我不叫setTextColor()的元素是红色的,其他都是灰色的,不管我选择(即使是相同的红了起来)。它的颜色
and other colors. While the elements on which I don't call setTextColor() are red, all others are grey, no matter which color I chose (even if it's the same red again).
这是为什么?
该文档是不是非常详细的关于这一点,但在调用的时候,你不能只使用R.color整数 setTextColor
。你需要调用 getResources()的getColor(R.color.YOURCOLOR)
来设置正确的颜色。
The documentation is not very verbose about this, but you cannot use just the R.color integer when calling setTextColor
. You need to call getResources().getColor(R.color.YOURCOLOR)
to set a color properly.
使用下面的以编程方式设置文本的颜色:
Use the following to set color of your text programmatically:
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));