如何以编程方式更改ListView中的TextView的背景颜色?

时间:2012-08-02 04:57:34

标签: android colors android-listview background-color textview

我有一个包含多个TextView项的ListView。此列表在运行时创建,大小可能不同。我想基于运行时生成的浮点值设置TextView项的背景。我正在使用ArrayAdapter。

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,ratios));  
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);

最后一行抛出NullPointerException。出于某种原因,我无法在listView中访问此TextView。如果我不知道运行时的颜色,我应该如何动态设置TextView的背景颜色?

3 个答案:

答案 0 :(得分:5)

只需创建自定义适配器。

参考此链接,

How to change color and font on ListView

答案 1 :(得分:0)

CustomAdapter.java:

public class CustomAdapter extends ArrayAdapter<String>{

    Context mContext;
    String list[];
    LayoutInflater mInflater;
    public static HashMap<Integer, String> idList=new HashMap<Integer,String>();

    public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
        super(context, textViewResourceId, objects);

        mContext=context;
        list=objects;
        mInflater=LayoutInflater.from(context);
        for(int i=0;i<list.length;i++){
            idList.put(i,"false");
        }
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.list_fruit,null);
            holder=new ViewHolder();

            holder.mTextView=(TextView)convertView.findViewById(R.id.mTextViewId);  
            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();

        idList.put(position, "true");           

        if(idList.get(position)=="true")
            holder.mTextView.setBackgroundColor(Color.GRAY);
        else
            holder.mTextView.setBackgroundColor(Color.WHITE);

        return convertView;
    }
    class ViewHolder{
        TextView mTextView;
    }
}

list_fruit.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mTextViewId" 
    android:background="#fff"
    android:textColor="#333"
    android:padding="5dip"/>

现在,

setListAdapter(new CustomAdapter(this, R.layout.list_fruit,ratios));  
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);

现在,无论点击哪个文字视图,都会获得灰色,而其他颜色则为白色。

答案 2 :(得分:0)

我假设你想要有选择地改变列表项的颜色。为此,您必须编写自己的自定义适配器并覆盖getView()方法。在getView()内部,您可以根据位置更改任何项目的颜色。

此链接可帮助您编写自定义适配器。

你的getView()看起来应该是这样的 -

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        StockQuoteView sqView = null;

        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.stock_quote_list_item, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sqView = new StockQuoteView();
            sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_symbol);
            sqView.quote = (TextView) rowView.findViewById(R.id.ticker_price);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sqView);
        } else {
            sqView = (StockQuoteView) rowView.getTag();
        }

        if(position == 3) {
            rowView.setBackgroundColor(#030303);
        }

        // Transfer the stock data from the data object
        // to the view objects
        StockQuote currentStock = stocks.get(position);
        sqView.ticker.setText(currentStock.getTickerSymbol());
        sqView.quote.setText(currentStock.getQuote().toString());

        return rowView;
    }

希望有所帮助。