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

标签 c# android xamarin xamarin-forms

我正在创建一个 ListView,它在 ViewCell 中包含一些简单的项目。

当我选择其中一项时,它变成橙色。当我单击并按住(打开上下文操作)时,它变成白色...

background color

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

使用这些名称我可以更改 ListView 样式。

android:colorPressedHighlight
android:colorLongPressedHighlight
android:colorFocusedHighlight
android:colorActivatedHighlight
android:activatedBackgroundIndicator

可以在 developer.android.com R.attr 上找到引用资料

colors

关于c# - 如何更改 ListView 上所选项目的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32490004/

相关文章:

c# - gridview WPF 中的按钮样式

c# - PetaPoco - 插入数据库时​​的事务隔离级别

c# - 解析 JSON 而不抛出错误异常?

android - 使 String 资源中的 phoneNumber 在对话框中可单击

c# - Android NfcV 读取标签总是在前面加上 0x00

ios - 运行 Storyboard 项目时,符合 Monotouch 键值编码标准的崩溃

c# - C# 泛型类型上下文中的类型标识是什么?

java - 非法状态异常 AsyncTask MediaPlayer

java - 将 Admob 添加到 libgdx

ios - Xamarin.iOS 设计器 - 创建 Action 转场不起作用