Android从java代码设置 TextView 颜色

标签 android android-layout

我有一个列表,我为此编写了一个自定义适配器。我想为此设置一些文本颜色(例如橙色代码#F06D2F)。我正在展示我的 getView() 方法的代码 fragment 。

TextView text = new TextView(this.context);
// text.setPadding(25, 5, 0, 0);

text.setBackgroundResource(R.drawable.back_horizontal);

// text.setClickable(false);
// text.setFocusable(false);
text.setEllipsize(TruncateAt.END);
text.setSingleLine(true);

// text.setTextColor(R.color.yellow);

text.setTextColor(R.color.Orange);
text.setGravity(Gravity.CENTER_VERTICAL);


helvetica_normal = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica.ttf");

text.setTypeface(helvetica_normal);
// text.setTextColor(R.color.yellow);



text.setText(objects[position]);

LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
manager.addView(text, layoutParams);

问题是我看不到设置为橙色的颜色。出了什么问题?

注意:上下文在构造函数和对象(字符串数组)中传递

感谢您的帮助

最佳答案

尝试这样,以下对我来说很好用

textview.setTextColor(this.getResources().getColor(R.color.orange));

关于Android从java代码设置 TextView 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4499208/

相关文章:

javascript - Android Collections.min NoSuchElementException

android - 什么样式/attr/drawable 用于反背景?

android - 每次设置 ImageView 位图时避免 onMeasure 和 onLayout

android - 将按钮并排放置

android - 无论屏幕大小和方向如何,如何使 Android 布局成比例

android - 如何在 android studio 中减小 Apk (.apk) 的大小

android - 动态壁纸可以在纵向模式下锁定屏幕吗?

javascript - 网站在不同的显示器分辨率下看起来不同 & CSS 不工作

android - 为什么 EditText.getText 在 Android 中显示为 null?

android - 如何获取在 xml 中实例化的自定义 View 的实例?