如何更改 ListView 上所选项目的颜色?
How do I change the color of a selected item on a ListView?
我正在创建一个 ListView,它在 ViewCell 中包含一些简单的项目。
当我 select 其中一项时,它变成橙色。当我单击并按住(打开上下文操作)时,它变成白色...
<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上自定义它。
为了使用我更改的主题 Droid/Properties/AssemblyInfo.cs
添加:
[assembly: Application(Theme = "@style/AppStyle.Light")]
我创建了一些文件:
Droid\Resources\values
colors.xml
包含我的主题的颜色定义:
<?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>
使用这些名称我可以更改列表视图样式。
android:colorPressedHighlight
android:colorLongPressedHighlight
android:colorFocusedHighlight
android:colorActivatedHighlight
android:activatedBackgroundIndicator
可以在 developer.android.com R.attr
上找到参考资料
派对迟到了,但问题已在 CollectionView
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#change-selected-item-color
中解决
<ContentPage.Resources>
<Style TargetType="Grid">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
我正在创建一个 ListView,它在 ViewCell 中包含一些简单的项目。
当我 select 其中一项时,它变成橙色。当我单击并按住(打开上下文操作)时,它变成白色...
<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上自定义它。
为了使用我更改的主题 Droid/Properties/AssemblyInfo.cs
添加:
[assembly: Application(Theme = "@style/AppStyle.Light")]
我创建了一些文件:
Droid\Resources\values
colors.xml
包含我的主题的颜色定义:
<?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>
使用这些名称我可以更改列表视图样式。
android:colorPressedHighlight
android:colorLongPressedHighlight
android:colorFocusedHighlight
android:colorActivatedHighlight
android:activatedBackgroundIndicator
可以在 developer.android.com R.attr
上找到参考资料派对迟到了,但问题已在 CollectionView
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#change-selected-item-color
<ContentPage.Resources>
<Style TargetType="Grid">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>