如何更改 ListView 上所选项目的颜色? [英] How do I change the color of a selected item on a ListView?

查看:35
本文介绍了如何更改 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:

机器人资源值

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆