更改文本颜色的ListView项目 [英] Change text color for ListView items

查看:102
本文介绍了更改文本颜色的ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何改变文字颜色为添加到的ListView 的项目。我需要在一定条件下,改变不同的行,以不同的文本颜色(例如,行0 =红色,ROW1 =白色,ROW3 =蓝色等)编程以改变在code中的颜色。设置文本颜色,在XML布局不会满足我的要求。这是我的code:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.listview);

    setListAdapter(新ArrayAdapter<字符串>(ListViewEx.this,
            R.layout.list_item_1,Global.availableDecks));

//这样的事情
//listview.getPosition(0).setTextColor(red);
//listview.getPosition(1).setTextColor(white);
//listview.getPosition(2).setTextColor(blue);
 

和我的XML:

 < XML版本=1.0编码=UTF-8&GT?;


    < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + /标记
    机器人:layout_width =match_parent
    机器人:layout_height =35dp
    机器人:textAppearance =机器人:ATTR / textAppearanceLarge
    机器人:TEXTSIZE =30像素
    机器人:layout_marginLeft =5像素
    机器人:单线=真
   />
 

解决方案

实现自己的ArrayAdapter,并覆盖 getView()方法:

 公共类适配器1扩展ArrayAdapter<字符串> {

    公共适配器1(上下文的背景下,INT渣油,ArrayList的<字符串>项目){
        超(背景下,渣油,项目);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        视图V = super.getView(位置,convertView,父母);
        如果(位置== 1){
            ((TextView中)V).setTextColor(Color.GREEN);
        }
        返回伏;
    }

}
 

不要忘了提供一个替代其他条款的颜色设置为默认,这样你就不会有问题,当你在处理一个循环一行。 然后,在你的活动:

  setListAdapter(新适配器1(ListViewEx.this,
            R.layout.list_item_1,Global.availableDecks));
 

How do I change the text color for the items that are added to a ListView. I need to change the colors programmatically in code based on certain conditions and changing different rows to different text colors(e.g. row 0 = red, row1= white, row3= blue etc). Setting a text color in the xml layout will not meet my requirements. Here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    setListAdapter(new ArrayAdapter<String>(ListViewEx.this,
            R.layout.list_item_1, Global.availableDecks));

//something like this 
//listview.getPosition(0).setTextColor(red);
//listview.getPosition(1).setTextColor(white);
//listview.getPosition(2).setTextColor(blue);

and my xml:

    <?xml version="1.0" encoding="utf-8"?>


    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30px"
    android:layout_marginLeft="5px"
    android:singleLine="true"
   />

解决方案

Implement your own ArrayAdapter and override the getView() method:

    public class Adapter1 extends ArrayAdapter<String> {

    public Adapter1(Context context, int resID, ArrayList<String> items) {
        super(context, resID, items);                       
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (position == 1) {
            ((TextView) v).setTextColor(Color.GREEN); 
        }
        return v;
    }

}

Don't forget to provide an alternative else clause to set the color to the default so you don't have problems when you're dealing with a recycled row. Then in your activity:

setListAdapter(new Adapter1(ListViewEx.this,
            R.layout.list_item_1, Global.availableDecks));

这篇关于更改文本颜色的ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆