【问题标题】:How to change ListView selected item's background color?如何更改 ListView 所选项目的背景颜色?
【发布时间】:2016-05-24 03:12:09
【问题描述】:

我在 xaml 文件中的ListView 结构写在下面。在这个列表视图中,我添加了一些列表项。当我单击此项目中的任何项目时,所选项目的背景颜色默认为橙色。我希望这个选定的 iem 的背景是黑色的。如何在 xaml 中实现这一点?

<ListView x:Name="LstMenu"
                HasUnevenRows="True">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
            <StackLayout Orientation="Vertical">
                <StackLayout Orientation="Vertical"`enter code here`
                             Padding="10">
                  <Label Text="{Binding ListItems}"
                         FontSize="15"
                         TextColor="White"
                         HorizontalTextAlignment="Start"
                         VerticalTextAlignment="Center" />
                </StackLayout>
                <Grid BackgroundColor="Black"
                      HeightRequest="1"/>
              </StackLayout>
              </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    我真的希望有一个很好的跨平台解决方案来解决这个问题,但到目前为止我还没有找到一个。这是来自Xamarin.Forms Forums 的几乎相同问题的链接。以一个Android项目为例,我一般通过在Resources目录下添加一个styles.xml文件来达到你想要的效果:

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
      <color name="CustomHighlight">@android:color/transparent</color>
      <style name="Theme.Splash" parent="android:Theme">
        <item name="android:windowBackground">@drawable/androidsplash</item> <!-- must be lowercase -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:backgroundDimEnabled">true</item>
      </style>
      <style name="CustomTheme" parent="android:Theme.Holo">
        <item name="android:icon">@android:color/transparent</item>
        <item name="android:colorPressedHighlight">@color/CustomHighlight</item>
        <item name="android:colorLongPressedHighlight">@color/CustomHighlight</item>
        <item name="android:colorFocusedHighlight">@color/CustomHighlight</item>
        <item name="android:colorActivatedHighlight">@color/CustomHighlight</item>
        <item name="android:activatedBackgroundIndicator">@color/CustomHighlight</item>
        <!--<item name="android:colorActivatedHighlight">@android:color/transparent</item>-->
      </style>
      <style name="ProgressBarStyle" parent="@android:style/Widget.ProgressBar.Horizontal"/>
    </resources>
    

    请注意,在这种情况下,我使用的是系统定义的颜色(透明),但您可以引用或定义您喜欢的任何颜色。然后在 MainActivity.cs 文件中添加对样式的引用:

    [Activity(Label = Constants.CompanyName,
              Theme = "@style/CustomTheme",
              Icon = "@drawable/icon",
              MainLauncher = false,
              ScreenOrientation = ScreenOrientation.Portrait,
              ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
       // your code here
    }
    

    我没有适用于 Windows 或 iOS 的方便示例,但概念是相同的;覆盖平台特定项目中的默认样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多