简体   繁体   English

更改 ListView 一行的文本颜色

[英]Change the text color of one row of a ListView

I have a ListView which is correctly filled and it displays all its items.我有一个正确填充的ListView ,它显示了它的所有项目。 I was wondering how could I change the color of one of its rows (index = 0).我想知道如何更改其中一行的颜色(索引 = 0)。 I was trying to use the ListView and reaching its child using listView.getChildAt(0) but I do not see any method to change the color of the text (just for the background)我试图使用ListView并使用listView.getChildAt(0)到达它的孩子,但我没有看到任何更改文本颜色的方法(仅用于背景)

Just in case is needed I post the method that I use to update the ListView以防万一需要我发布我用来更新ListView

String[] aux = new String[namesList.size()];
            namesList.toArray(aux);
            arrayAdapter = new ArrayAdapter<>(getActivity(), R.layout.row, aux);
            listView.setAdapter(arrayAdapter);
            if(from)
                listView_address.getChildAt(0) // Not a method which I could use
            setSelectNameListeners();

You cannot change the color of a String, however you can change color of a textview.你不能改变 String 的颜色,但是你可以改变 textview 的颜色。 A String only tells what to display, a textView will be responsible for how to display the information, now to change the color of the text you'll need a custom adapter and make that adapter return you the items, you get the 1st item and change it's color. String 只告诉要显示什么, textView 将负责如何显示信息,现在要更改文本的颜色,您将需要一个自定义适配器并使该适配器返回您的项目,您将获得第一个项目和改变它的颜色。

Yo can try this:你可以试试这个:

        String[] aux = new String[namesList.size()];
        namesList.toArray(aux);
        arrayAdapter = new ArrayAdapter<>(getActivity(), R.layout.row, aux){
             @Override
            public View getView(int position, View convertView, ViewGroup parent){
              if(position == 0){   
               // Get the Item from ListView
               View view = super.getView(position, convertView, parent);

               // Initialize a TextView for ListView each Item
               TextView tv = (TextView) view.findViewById(android.R.id.text1);

               // Set the text color of TextView (ListView Item)
               tv.setTextColor(Color.RED);

               // Generate ListView Item using TextView
               return view;
               }   
             }
        };
        listView.setAdapter(arrayAdapter);
        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM