2017-10-31 3 views
1

Ich suche diese Farbe nur auf alle Schalter anzuwenden. Aber standardmäßig wird colorAccent anstelle dieses Themas für den Wechsel verwendet.
Ich kann die Farbe des Schalters nicht ändern

Gerät: Marshmallow.

Layout:

<Switch 
      android:id="@+id/soundSwitch" 
      style="@style/SwitchStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginBottom="@dimen/large_space" 
      android:layout_marginRight="@dimen/medium_space" 
      android:layout_marginTop="@dimen/large_space" 
      android:checked="true" 
      /> 

Arten-v21:

<style name="SwitchStyle" parent="Theme.AppCompat.Light"> 
     <!-- active thumb & track color (30% transparency) --> 
     <item name="android:colorControlActivated">@color/switch_color</item> 

     <!-- inactive thumb color --> 
     <item name="colorSwitchThumbNormal">#f1f1f1</item> 

     <!-- inactive track color (30% transparency) --> 
     <item name="android:colorForeground">#42221f1f</item> 
    </style> 

Was mache ich falsch?

+0

, wenn Sie in diesem Kampf und scheitern Sie nur das Überschreiben der colorAccent ' Ihre Farbe' – Xenolion

Antwort

2

Sie Misch Arten und Themen .

Diese Attribute sind Themenattribute sie so definieren zusammen in einem Thema Overlay:

res/Werte/styles.xml (nicht Werte-v21)

<style name="ThemeOverlay.MySwitch" parent=""> 
    <item name="android:colorControlActivated">@color/switch_color</item> 
    <item name="android:colorSwitchThumbNormal">#f1f1f1</item> 
    <item name="android:colorForeground">#42221f1f</item> 
</style> 

<style name="ThemeOverlay.MySwitchCompat" parent=""> 
    <item name="colorControlActivated">@color/switch_color</item> 
    <item name="colorSwitchThumbNormal">#f1f1f1</item> 
    <item name="android:colorForeground">#42221f1f</item> 
</style> 

Und dann dies gilt Thema Overlay auf dem Schalter:

res/layout/layout.xml

<Switch android:theme="@style/ThemeOverlay.MySwitch"/> 

<android.support.v7.widget.SwitchCompat android:theme="@style/ThemeOverlay.MySwitchCompat"/> 

eine der beiden Varianten wählen:

  • Switch verfügbar seit API 21, alle Themen Attribute werden mit dem Präfix android:
  • SwitchCompat in appcompat-v7 Support-Bibliothek werden einige Themen Attribute nicht als Präfix (stellen Sie sicher, dass Sie wissen, was).
0

Fügen Sie dies zur style.xml für das Switch-Styling hinzu.

<style name="SwitchThemeOverlay" parent=""> 
    <!-- active thumb & track color (30% transparency) --> 
    <item name="colorControlActivated">#00c853</item> 

    <!-- inactive thumb color --> 
    <item name="colorSwitchThumbNormal">#CC0000</item> 

    <!-- inactive track color (30% transparency) --> 
    <item name="android:colorForeground">#666666 
    </item> 
</style> 

und in der XML-Anwendung

<android.support.v7.widget.SwitchCompat 
     android:id="@+id/switch_desc" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textColor="@color/colorPrimaryDark" 
     android:padding="5dp" 
     android:checked="false" 
     android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" 
     android:theme="@style/SwitchThemeOverlay" 
     android:layout_marginLeft="10dp" 
     android:text="Description"/> 
+0

I changed „Style“ eine kurze Alternative bekommen kann „ThemeOverlay“ besser zu beschreiben, was Ereignis. –

1

Wahrscheinlich können Sie versuchen, zusammen mit android.support.v7.widget.SwitchCompat statt Switch und android:[email protected]/SwitchStyle statt style="@style/SwitchStyle"

Verwandte Themen