2016-05-22 249 views
0

我的ListView xaml文件中的結構寫在下面。在這個列表視圖中,我添加了一些列表項。當我點擊這些項目中的任何項目時,所選項目的背景顏色默認爲橙色。我希望這個選定的IE背景是黑色的。我如何在xaml中實現這一點?如何更改ListView所選項目的背景顏色?

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

回答

0

我真的希望有一個很好的跨平臺解決這個問題,但迄今爲止我還沒有找到一個。以下是Xamarin.Forms Forums中幾乎相同問題的鏈接。

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

請注意,我在這個使用系統正定義的顏色(透明):使用Android項目爲例,我通常通過添加styles.xml文件到資源目錄實現你正在尋找的效果情況下,但你可以參考或限定任何顏色,你like.Then在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的一個方便的樣本,但概念是一樣的;覆蓋特定於平臺的項目中的默認樣式。

相關問題