【问题标题】:How do you change the background color of a TextView that is inside a ListView Programmatically?如何以编程方式更改 ListView 内的 TextView 的背景颜色?
【发布时间】:2012-07-31 01:25:36
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    只需找到 TextView 为:

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

    然后做任何你想做的事,例如:

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

    编辑:

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

    【讨论】:

    • 但他在 ListView 中这么说......这意味着您必须创建一个自定义适配器,而不是使用标准适配器,例如 setListAdapter。最好的方法是创建一个扩展 ListAdapter 的公共类,然后你可以覆盖 getView 的定义,在那里你可以像 Yaqub 建议的那样对你的文本视图做任何你想做的事情
    • 没错。但他仍然可以使用setListAdapter()。我猜。
    • 在定义我自己的唯一自定义适配器后,使用 setListAdapter 会引发 NullPointerException
    【解决方案2】:

    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);
    

    现在,无论哪个文本视图被点击,都会变成灰色,其他的都是白色。

    【讨论】:

    • 这是我正在寻找的东西,不幸的是我仍然收到错误:1.变量 names 未定义,所以我创建了一个 String[] names 变量CustomAdapter, 2. 调用 setListAdapter() 方法时,传入的 CustomAdapter 必须删除其 模板,3. 我必须从 android 中删除 android. .R.layout.list_fruit 参数传入。当我最终编译它时,setListAdapter 行崩溃并出现 NullPointerException
    • 现在请检查代码。我做了一些修复。我错过了之前代码中变量名的一些更改。现在它似乎已经为你准备好了。不过,如果有任何问题,请告诉我发生。
    【解决方案3】:

    我假设您想有选择地更改列表项的颜色。为此,您必须编写自己的自定义适配器并覆盖 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;
        }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案4】:

      只需创建自定义适配器。

      参考这个链接,

      How to change color and font on ListView

      【讨论】:

      • 是的,在仔细查看这篇文章之后,这就是解决方案。谢谢拉杰什瓦兰。我刚刚爱上了这个网站。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多