如何更改ListView上所选项目的颜色? [英] How do I change the color of a selected item on a ListView?
问题描述
我正在创建一个ListView,它在ViewCell中具有一些简单的项.
I'm creating a ListView that has some simple items inside a ViewCell.
当我选择一项时,它变成橙色.当我单击并按住(打开上下文操作)时,它变成白色...
When I select one of the items it becomes orange. When I click and hold (to open the context actions) it becomes white...
<ListView ItemsSource="{Binding Items}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Delete" />
</ViewCell.ContextActions>
<StackLayout Orientation="Horizontal" Padding="20">
<StackLayout HorizontalOptions="StartAndExpand">
<Label Text="{Binding Name}" FontSize="Large" FontAttributes="Bold" />
<Label Text="{Binding Description}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如何自定义这些颜色?
推荐答案
我发现我必须直接在Android上对其进行自定义.
I found out that I have to customize it directly on Android.
要使用主题,我更改了Droid/Properties/AssemblyInfo.cs
添加:
To use the theme I changed Droid/Properties/AssemblyInfo.cs
adding:
[assembly: Application(Theme = "@style/AppStyle.Light")]
然后我在以下位置创建了一些文件
And I created some files on:
Droid \ Resources \ values
Droid\Resources\values
colors.xml
包含我主题的颜色定义:
colors.xml
contains the color definitions for my theme:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<color name="ListViewSelected">#96BCE3</color>
<color name="ListViewHighlighted">#E39696</color>
</resources>
styles.xml
包含主题设置:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="AppStyle.Light" parent="android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPressedHighlight">@color/ListViewSelected</item>
<item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
<item name="android:colorFocusedHighlight">@color/ListViewSelected</item>
<item name="android:colorActivatedHighlight">@color/ListViewSelected</item>
<item name="android:activatedBackgroundIndicator">@color/ListViewSelected</item>
</style>
</resources>
使用这些名称,我可以更改列表视图的样式.
Using these names I can change the listview style.
android:colorPressedHighlight
android:colorLongPressedHighlight
android:colorFocusedHighlight
android:colorActivatedHighlight
android:activatedBackgroundIndicator
参考可在 developer.android.com R.attr
这篇关于如何更改ListView上所选项目的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!