2

我有一个包含多个 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 的背景颜色?

4

4 回答 4

5

只需创建自定义适配器。

参考这个链接,

如何在 ListView 上更改颜色和字体

于 2012-08-02T05:25:46.103 回答
2

只需找到 TextView 为:

TextView myTextView = (TextView)findViewById(R.id.yourTextViewId);

并做任何你想做的事情,例如:

myTextView.setTextColor(color); 
myTextView.setBackgroundColor(color);

编辑

请在这个网站上找到如何实现“android自定义适配器”?

于 2012-08-02T05:08:13.967 回答
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);

现在,无论单击哪个文本视图,都将变为灰色,其他为白色。

于 2012-08-02T05:11:24.240 回答
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;
    }

希望有帮助。

于 2012-08-02T05:24:33.040 回答