英文:
Change the text color of one row of a ListView
问题
我有一个 ListView
,它被正确地填充了,并且显示出了所有的项目。我想知道如何改变其中一行(索引 = 0)的颜色。我尝试使用 ListView
,并通过 listView.getChildAt(0)
达到它的子项,但我没有看到任何改变文本颜色的方法(仅限于背景颜色)。
以防万一需要,我会贴出我用来更新 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) // 没有可用的方法
setSelectNameListeners();
英文:
I have a ListView
which is correctly filled and it displays all its items. I was wondering how could I change the color of one of its rows (index = 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)
Just in case is needed I post the method that I use to update the 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();
答案1
得分: 2
你不能改变字符串的颜色,但可以改变文本视图的颜色。
字符串只是告诉要显示什么,文本视图将负责如何显示信息,现在要改变文本的颜色,您需要一个自定义适配器,并使该适配器返回您所需的项目,获取第一个项目并更改其颜色。
英文:
You cannot change the color of a String, however you can change color of a 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.
答案2
得分: 1
您可以尝试以下代码:
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) {
// 从ListView获取项目
View view = super.getView(position, convertView, parent);
// 为每个ListView项目初始化一个TextView
TextView tv = (TextView) view.findViewById(android.R.id.text1);
// 设置TextView(ListView项目)的文本颜色
tv.setTextColor(Color.RED);
// 使用TextView生成ListView项目
return view;
}
}
};
listView.setAdapter(arrayAdapter);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论