You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中如何用Java修改ListView内所有文本颜色?

如何用Java修改ListView中所有TextView的颜色?

现有代码

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String[] programName = {"ex1", "ex2", "ex3"};
    ListView lvProgram;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvProgram = findViewById(R.id.listView);

        ProgramAdapter programAdapter = new ProgramAdapter(this, programName);
        lvProgram.setAdapter(programAdapter);
    }
}

ProgramAdapter.java

public class ProgramAdapter extends ArrayAdapter<String> {
    Context context;
    String[] programName;

    public ProgramAdapter(Context context, String[] programName) {
        super(context, R.layout.single_item2, R.id.titulo, programName);
        this.context = context;
        this.programName = programName;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View singleItem = convertView;
        ProgramViewHolder holder = null;
        if(singleItem == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            singleItem = layoutInflater.inflate(R.layout.single_item2, parent, false);
            holder = new ProgramViewHolder(singleItem);
            singleItem.setTag(holder);
        }
        else{
            holder = (ProgramViewHolder) singleItem.getTag();
        }
        holder.programTitle.setText(programName[position]);
        return singleItem;
    }

    private static class ProgramViewHolder {
        TextView programTitle;

        ProgramViewHolder(View itemView) {
            programTitle = itemView.findViewById(R.id.titulo);
        }
    }
}

问题分析

直接调用findViewById(R.id.TextView1).setBackgroundColor(Color.BLACK)只能修改当前可见的某一个TextView颜色,这是因为ListView采用View复用机制:只有屏幕上显示的item才会被加载到布局中,滚动时会复用之前创建的View。直接在Activity中查找控件只能拿到当前布局里存在的那个TextView,无法覆盖所有item。

解决方案

方案1:在Adapter的getView中直接设置颜色

每个item在创建或复用时都会执行getView方法,在这里设置颜色可以确保所有item的TextView都被修改:

修改ProgramAdapter的getView方法,在holder.programTitle.setText(...)之后添加颜色设置代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View singleItem = convertView;
    ProgramViewHolder holder = null;
    if(singleItem == null){
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        singleItem = layoutInflater.inflate(R.layout.single_item2, parent, false);
        holder = new ProgramViewHolder(singleItem);
        singleItem.setTag(holder);
    }
    else{
        holder = (ProgramViewHolder) singleItem.getTag();
    }
    holder.programTitle.setText(programName[position]);
    // 设置TextView背景色
    holder.programTitle.setBackgroundColor(Color.BLACK);
    // 如果需要修改文字颜色,替换为下面这行
    // holder.programTitle.setTextColor(Color.WHITE);
    return singleItem;
}

方案2:动态修改所有item的颜色(如点击按钮触发)

如果需要在运行时动态修改颜色,可给Adapter添加颜色变量和设置方法,再刷新Adapter:

  1. 修改ProgramAdapter:
public class ProgramAdapter extends ArrayAdapter<String> {
    Context context;
    String[] programName;
    private int textBgColor = Color.WHITE; // 默认背景色

    public ProgramAdapter(Context context, String[] programName) {
        super(context, R.layout.single_item2, R.id.titulo, programName);
        this.context = context;
        this.programName = programName;
    }

    // 设置背景色的方法
    public void setTextBgColor(int color) {
        this.textBgColor = color;
        notifyDataSetChanged(); // 通知Adapter刷新所有item
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View singleItem = convertView;
        ProgramViewHolder holder = null;
        if(singleItem == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            singleItem = layoutInflater.inflate(R.layout.single_item2, parent, false);
            holder = new ProgramViewHolder(singleItem);
            singleItem.setTag(holder);
        }
        else{
            holder = (ProgramViewHolder) singleItem.getTag();
        }
        holder.programTitle.setText(programName[position]);
        holder.programTitle.setBackgroundColor(textBgColor); // 使用变量设置颜色
        return singleItem;
    }

    private static class ProgramViewHolder {
        TextView programTitle;

        ProgramViewHolder(View itemView) {
            programTitle = itemView.findViewById(R.id.titulo);
        }
    }
}
  1. 在MainActivity中调用设置方法:
// 示例:按钮点击触发颜色修改
Button changeColorBtn = findViewById(R.id.change_color_btn);
changeColorBtn.setOnClickListener(v -> {
    programAdapter.setTextBgColor(Color.BLACK);
});

内容的提问来源于stack exchange,提问作者The Crypto Cup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:40:43