在Android上更改Xamarin.Forms Listview中的默认TextColor

Chr*_*ris 2 listview xamarin.android xamarin android-styles xamarin.forms

我试图TextColor在Andoid上更改Xamarin.Forms ListView中的默认值.

ListView非常简单:

List<string> cities = new List<string> { "Berlin", "Bonn", "Braunschweig", "Bremen" };
ListView listView = new ListView();
listView.ItemsSource = cities;
Content = listView;
Run Code Online (Sandbox Code Playgroud)

在设备上它看起来像这样:

在此输入图像描述

我想知道的是,TextColor它将是黑色的.据我所知,Xamarin Forms CustomRendererAndroid.Resource.Layout.SimpleListItem1为每个项目生成一个.

SimpleListItem1使用以下textAppearance:

android:textAppearance="?android:attr/textAppearanceListItemSmall"
Run Code Online (Sandbox Code Playgroud)

textAppearanceListItemSmall使用属性textAppearanceMedium进行渲染,如此处所示.

所以我已经将颜色和主题添加到资源中:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <color name="Black">#000000</color>
</resources>


<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:textAppearanceMedium">@style/MyDefaultTextAppearanceM</item>
  </style>

  <style name="MyDefaultTextAppearanceM" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">@color/Black</item>
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

我还将主题添加到我的Activity的属性(Theme = "@style/MyTheme)主题基本上工作.随着<item name="android:colorActivatedHighlight">@color/Blue4</item>我可以改变HighlightColor.

那么如何才能将ListView中的文本颜色变为黑色?我做错了什么?

小智 7

在设置内容之前尝试添加此内容:

var template = new DataTemplate(typeof(TextCell));
template.SetValue(TextCell.TextColorProperty, Color.Black);
listView.ItemTemplate = template;
Run Code Online (Sandbox Code Playgroud)

  • 您还必须添加template.SetBinding(TextCell.TextProperty,"."); 然后它正在工作. (3认同)