【问题标题】:How to change ListView item text color in Xamarin.Android?如何在 Xamarin.Android 中更改 ListView 项目文本颜色?
【发布时间】:2018-12-09 23:02:28
【问题描述】:

我有一个简单的 ListView,如下所示:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/asimplelistView1"

 />

我正在使用数组 ArrayAdapter 绑定这个 ListView:

ArrayAdapter<string> arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1,mItems);

我的应用辅助文本颜色为白色(#FFFFFF),主题为 android:Theme.Material.Light..

我的问题是:我需要将项目文本颜色更改为灰色,因为项目文本颜色在白色背景上不可见。

在 Java 中我们可以这样做:

// Create a List from String Array elements
    List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));

    // Create an ArrayAdapter from List
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, fruits_list){
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            // Get the Item from ListView
            View view = super.getView(position, convertView, parent);

            // Initialize a TextView for ListView each Item
            TextView tv = (TextView) view.findViewById(android.R.id.text1);

            // Set the text color of TextView (ListView Item)
            tv.setTextColor(Color.RED);

            // Generate ListView Item using TextView
            return view;
        }
    };

如何在 C# 中使用 xamarin.android 实现此目的

【问题讨论】:

  • 尝试使用自定义适配器而不是 ArrayAdapter,您可以像在 Java 中那样覆盖 GetView 方法
  • @Dmitriy Kaluzhin,感谢您的回复。我知道自定义适配器,我需要通过这种方式知道,你能帮忙吗?
  • 您可以尝试在 ArrayAdapter 构造函数而不是 SimpleExpandableListItem1 中发送您的自定义视图,或者创建另一个适配器继承的 ArrayAdapter 类,您可以在其中覆盖 GetView 方法

标签: xamarin xamarin.android objectlistview


【解决方案1】:

我遇到了同样的情况,并且能够在 styles.xml 文件上设置一个新的“项目”条目来修复它:

@color/colorAccent

颜色值可以是对您的 colors.xml 的任何条目引用或十六进制值

【讨论】:

    猜你喜欢
    • 2012-07-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多