Android Studio 如何使用JAVA在列表视图Android工作室中更改颜色文本?

ifmq2ha2  于 2022-12-04  发布在  Android
关注(0)|答案(2)|浏览(144)

我需要使用JAVA,从LISTVIEW更改所有文本视图的颜色。
MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

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]);

        }

}

我的尝试:

findViewById(R.id.TextView1).setBackgroundColor(Color.BLACK);

我的尝试甚至工作,但它只改变了一些文本视图的颜色和随机时间,它没有改变所有的方式,我想要的。

yhuiod9q

yhuiod9q1#

当您需要更改文本视图的颜色时-

holder.programTitle.setTextColor(Color.RED)

当您需要更改文本视图的背景颜色时-

holder.programTitle.setBackgroundColor(Color.RED)
qni6mghb

qni6mghb2#

试试这个

保持器.程序标题.背景颜色(颜色.黑色);

相关问题