2

我想更改Andoid上的Xamarin.Forms ListView中的默認TextColor更改Xamarin.Forms Android上的默認TextColor Android上的Listview

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

所以我已經添加了色彩和主題,以resouces:

<?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的解決方案非常簡單 - 只需在ImageCell上設置TextColor即可。像這樣:

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