You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin Android中修改ListView列表项颜色?附现有代码

Hey there! Let's sort out how to change the ListView item colors in your Xamarin Native Android project. I've got two easy-to-implement methods for you:

Method 1: Use a Custom Layout File

This is the most straightforward approach if you just need static color changes for your list items.

  1. First, create a new layout file named list_item_custom.xml in your Resources/Layout folder. This will define the look of each list item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_item_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:textColor="#FF4081" <!-- Replace with your desired text color -->
    android:background="#F5F5F5" <!-- Replace with your desired background color -->
    android:textSize="16sp"/>
  1. Update your existing code to use this custom layout instead of the system default SimpleListItem1:
mlistView = FindViewById<ListView>(Resource.Id.lista_clientes);
mItems = new List<string>();
for (int i = 0; i < dt_lista.Rows.Count; i++) {
    mItems.Add(dt_lista.Rows[i][1].ToString());
}
// Use the custom layout and specify the TextView ID where text should be placed
mAdapter = new ArrayAdapter<string>(this, Resource.Layout.list_item_custom, Resource.Id.tv_item_text, mItems);
mlistView.Adapter = mAdapter;

Method 2: Create a Custom ArrayAdapter (For More Flexibility)

If you need dynamic color changes (like alternating row colors) or plan to add more UI elements later, a custom adapter is the way to go.

  1. Create a new class that inherits from ArrayAdapter<string>:
public class CustomListAdapter : ArrayAdapter<string>
{
    private readonly Context _context;
    private readonly List<string> _items;

    public CustomListAdapter(Context context, List<string> items) : base(context, 0, items)
    {
        _context = context;
        _items = items;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        // Reuse existing views to improve performance (critical for long lists!)
        View listItem = convertView ?? LayoutInflater.From(_context).Inflate(Android.Resource.Layout.SimpleListItem1, parent, false);
        
        TextView textView = listItem.FindViewById<TextView>(Android.Resource.Id.Text1);
        textView.Text = _items[position];
        
        // Set your desired text and background colors
        textView.SetTextColor(Android.Graphics.Color.ParseColor("#FF4081"));
        // Uncomment below for static background color
        // listItem.SetBackgroundColor(Android.Graphics.Color.ParseColor("#F5F5F5"));
        
        // Example: Alternate row colors for better readability
        if (position % 2 == 0)
        {
            listItem.SetBackgroundColor(Android.Graphics.Color.LightGray);
        }
        else
        {
            listItem.SetBackgroundColor(Android.Graphics.Color.White);
        }
        
        return listItem;
    }
}
  1. Swap out your default adapter with this custom one in your Activity code:
mlistView = FindViewById<ListView>(Resource.Id.lista_clientes);
mItems = new List<string>();
for (int i = 0; i < dt_lista.Rows.Count; i++) {
    mItems.Add(dt_lista.Rows[i][1].ToString());
}
mAdapter = new CustomListAdapter(this, mItems);
mlistView.Adapter = mAdapter;

Pro Tip

For better maintainability, define your colors in the Resources/values/colors.xml file instead of hardcoding hex values. For example:

<color name="primary_text">#FF4081</color>
<color name="light_background">#F5F5F5</color>

Then reference them in code like this:

textView.SetTextColor(Resources.GetColor(Resource.Color.primary_text));
listItem.SetBackgroundColor(Resources.GetColor(Resource.Color.light_background));

内容的提问来源于stack exchange,提问作者Kennyto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:54:35