加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:07:11 所属栏目:百科 来源:网络整理
导读:我正在创建一个ListView,在ViewCell中有一些简单的项目. 当我选择其中一个项目时,它变成橙色.当我点击并按住(打开上下文动作)它变成白色… ListView ItemsSource="{Binding Items}" HasUnevenRows="True" ListView.ItemTemplate DataTemplate ViewCell ViewC
我正在创建一个ListView,在ViewCell中有一些简单的项目.

当我选择其中一个项目时,它变成橙色.当我点击并按住(打开上下文动作)它变成白色…

<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")]

我创建了一些文件:

DroidResourcesvalues

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读