在Android中的库22中,以编程方式设置TextView中的setTextColor。

10 浏览
0 Comments

在Android中的库22中,以编程方式设置TextView中的setTextColor。

最近我对我的应用程序做了一点改动,但是由于某种我不理解的原因,“setTextColor”方法似乎不再起作用。

在我的XML中,我有一个ListView,并且我在这个ListView中以编程方式添加TextView。

XML:

   
    

Java:

textView = new TextView(getContext());
    textView.setText("some text");
    textView.setTextSize(20f);
    textView.setGravity(Gravity.CENTER);
    textView.setTextColor(Color.BLACK);
    textView.setTextAppearance(getContext(), android.R.style.TextAppearance_Medium);
    addView(textView);

但是不管我怎么做,这个文本都是白色的。

为什么?

0
0 Comments

问题:在Android中,在库22中以编程方式设置TextView的文本颜色时出现问题。

原因:在库22中,使用`setTextColor(Color.BLACK)`方法设置文本颜色可能会导致问题。

解决方法:改用`setTextColor(Color.parseColor("#000000"))`方法来设置文本颜色。

0
0 Comments

问题出现的原因:在使用Android库的22版本中,使用`textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));`来设置TextView的文字颜色时,可能会出现无法生效的情况。

解决方法:在库的22版本中,可以使用以下代码来设置TextView的文字颜色:

textView.setTextColor(context.getResources().getColor(R.color.YOURCOLOR));

注意:以上解决方法仅适用于Android库的22版本。如果使用的是支持库的23版本或更高版本,则应使用以下代码:

textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));

参考链接:TextView setTextColor() not working

0
0 Comments

在Android中,使用库22中的程序编程方式设置TextView中的文本颜色。

问题的原因是在调用setTextAppearance后,调用setTextColor会导致问题。解决方法是在调用setTextColor之前调用setTextAppearance。以下是一个可行的代码示例:

TextView textView = new TextView(this);
textView.setText("some text");
textView.setTextSize(20f);
textView.setGravity(Gravity.CENTER);
// textView.setTextColor(Color.RED);
textView.setTextAppearance(this, android.R.style.TextAppearance_Medium);
textView.setTextColor(Color.RED);
// setContentView(textView);

对于这个问题的真正原因我不清楚。感谢你,这解决了我的问题。由于我仍在使用库22,所以这是其他解决方法之外的一个解决方案。

0