2

Я пытаюсь изменить значение по умолчанию TextColor в Xamarin.Forms ListView на Andoid.Изменение по умолчанию TextColor в Xamarin.Forms Listview на Android

элемент управления ListView довольно прост:

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

На устройстве он выглядит следующим образом:

enter image description here

Что wnat иметь, является то, что TextColor будет черным. Насколько я понимаю, Xamarin Forms CustomRenderer будет создан для каждого элемента Android.Resource.Layout.SimpleListItem1.

SimpleListItem1 использует следующую textAppearance:

android:textAppearance="?android:attr/textAppearanceListItemSmall" 

textAppearanceListItemSmall использует атрибут textAppearanceMedium для визуализации, как вы можете видеть here.

Так я добавил цвета и темы для ресы:

<?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> 

Я также добавил тему к атрибуту для моей деятельности (Theme = "@style/MyTheme) Тема в основном работает. С <item name="android:colorActivatedHighlight">@color/Blue4</item> Я могу изменить HighlightColor.

Итак, как я могу получить цвет текста в черном ListView? Что я делаю неправильно?

ответ

4

Попробуйте добавить это, прежде чем установить содержание:

var template = new DataTemplate(typeof(TextCell)); 
template.SetValue(TextCell.TextColorProperty, Color.Black); 
listView.ItemTemplate = template; 
+2

Вы также должны добавить template.SetBinding; (TextCell.TextProperty "") то он работает. – Chris

0

Поздний разговор, но, возможно, это будет другим вниз по дороге.

Решение на основе Xaml, очень простое - Просто установите TextColor на ImageCell. Как так:

<ContentPage.Content> 
     <StackLayout VerticalOptions="FillAndExpand"> 
      <ListView x:Name="listView" VerticalOptions="FillAndExpand" 
       SeparatorVisibility="None"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <ImageCell TextColor="Black" Text="{Binding Title}" 
          ImageSource="{Binding IconSource}"/> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 
0
[assembly: ExportRenderer(typeof(MyTextCell), typeof(MyTextCellRenderer))] 
namespace MyNamespace.Droid.Renderers 
{ 
    public class MyTextCellRenderer : TextCellRenderer 
    { 
     protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context) 
     { 
      Android.Views.View result = base.GetCellCore(item, convertView, parent, context); 

      (result as BaseCellView).SetDefaultMainTextColor(Color.Black); 

      return result; 
     } 
    } 
} 
Смежные вопросы