简体   繁体   English

如何更改 ListView 上所选项目的颜色?

[英]How do I change the color of a selected item on a ListView?

I'm creating a ListView that has some simple items inside a ViewCell.我正在创建一个 ListView,它在 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>

How can I customize these colors?如何自定义这些颜色?

I found out that I have to customize it directly on Android.我发现我必须直接在Android上自定义它。

To use the theme I changed Droid/Properties/AssemblyInfo.cs adding:为了使用主题,我更改了Droid/Properties/AssemblyInfo.cs添加:

[assembly: Application(Theme = "@style/AppStyle.Light")]

And I created some files on:我在以下位置创建了一些文件:

Droid\\Resources\\values机器人\\资源\\值

colors.xml contains the color definitions for my theme: colors.xml包含我的主题的颜色定义:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="ListViewSelected">#96BCE3</color>
  <color name="ListViewHighlighted">#E39696</color>
</resources>

styles.xml contains the theme settings: 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

References can be found on developer.android.com R.attr参考资料可以在developer.android.com R.attr找到

颜色

Late to the party but the problem has been solved in the CollectionView https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#change-selected-item-color聚会迟到但问题已在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM