【问题标题】:Change textview color which is adapted in a listview from custom layout [duplicate]从自定义布局更改在列表视图中调整的文本视图颜色[重复]
【发布时间】:2018-11-24 10:16:04
【问题描述】:

我有一个 ListView,其中包含多个位于自定义布局中的 TextView 项目。此列表是在运行时创建的,大小可能会有所不同。我为 ListView 使用了 SimpleAdapter

我想根据某些条件更改 TextViews 颜色。

我试过了,但它没有改变任何东西

    LayoutInflater inflater = LayoutInflater.from(Measurements.this);
    View view = inflater.inflate(R.layout.custom_row_view,null);
    TextView before = (TextView)view.findViewById(R.id.before_t);
    TextView after = (TextView)view.findViewById(R.id.after_t);
    TextView fasting = (TextView)view.findViewById(R.id.fasting_t);

    while (data.moveToNext()) {
    if(data.getDouble(2)>110) before.setTextColor(Color.RED);
    }

这是列表视图适配器

 SimpleAdapter sa = new SimpleAdapter(this, list,
            R.layout.custom_row_view,
            new String[]{"line1", "line2", "line3", "line4"},
            new int[]{R.id.date, R.id.before_t, R.id.after_t,R.id.fasting_t});
    listview.setAdapter(sa);

您能指出解决方案吗?

【问题讨论】:

  • 应该是R.color.RED我想是的
  • 您需要更改自定义适配器类中的颜色...您没有随问题发布。
  • 不是自定义适配器@Barns
  • 为什么会这样? @谷仓
  • 因为您正在尝试引用一个视图,该视图显示在 ListView 的行中,该行正在“幕后”构建,您需要在某些视图被创建时更改它们的属性已创建。

标签: android listview textview android-custom-view


【解决方案1】:

使用这行代码:-

1> ContextCompat.getColor(context, R.color.RED);

2>getActivity().getResources().getColor(android.R.color.white);

这行代码对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多