Android 更改列表视图项文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20809272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android change listview item text color
提问by Mohammad abumazen
i'm trying to change some items text color (or backgroung color) in a list view based on a flag . after a long search i didn't find out how to do it , i'm calling the below loop after specific action to change the color:
我正在尝试根据标志在列表视图中更改某些项目的文本颜色(或背景颜色)。经过长时间的搜索,我没有找到如何去做,我在特定操作后调用以下循环来更改颜色:
ListView _listView = (ListView) findViewById(R.id.listView2);
for (int i=0;i<=_listView.getCount();i++)
{
if(Falg == True)
{
//here i want to change the list view item color or backgroud color
}
}
回答by Adnan Mulla
You can override the getView method of Array adapter and change the color:
您可以覆盖 Array 适配器的 getView 方法并更改颜色:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
myList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if (flag == True) {
text.setTextColor(Color.BLACK);
}
return view;
}
};
回答by pawegio
You can do it directly in your custom Adapter
.
您可以直接在您的自定义Adapter
.
You can inflate row layout in this method and dynamically change view colors and other stuff.
您可以在此方法中扩展行布局并动态更改视图颜色和其他内容。
回答by Gaurav Setia
Create a Custom Layout, define your color in that layout.
创建自定义布局,在该布局中定义您的颜色。
Like this:
像这样:
<? xml version="1.0" encoding=" utf-8 "? >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/white" // Here You define your desired color
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"/>
Use this layout in your adapter, like this:
在您的适配器中使用此布局,如下所示:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, R.layout.custom_textview, info);
lv.setAdapter(adapter);
This way you don't need to use custom adapter.
这样您就不需要使用自定义适配器。
回答by Ksharp
You can set font color style .... like
您可以设置字体颜色样式....喜欢
textView.setText(Html.fromHtml("<b><u>"+string+"<b><u>"));
when you populate the text into your listview.
当您将文本填充到列表视图中时。