2017-10-31 16 views
1

この色はすべてのスイッチにのみ適用します。しかし、デフォルトではスイッチのテーマではなく、colorAccentとなっています。
スイッチの色を変更できません

デバイス:marshmallow。

レイアウト:

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

スタイル-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> 

私が間違っているのか?

+0

あなたはその中で苦労して失敗した場合、あなただけのcolorAccent '<項目名=「colorAccent」>をオーバーライドすることで、短い代替を得ることができ、あなたの色' – Xenolion

答えて

2

の代わりにSwitchandroid:[email protected]/SwitchStyleandroid.support.v7.widget.SwitchCompatを使用して試すことができます。

のres /値/のstyles.xml(ない値-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> 

そして、これを適用します。

これらの属性は、テーマははとてもテーマオーバーレイでそれらを一緒に定義する属性ですスイッチのテーマオーバーレイ:

res/layout/layout.xml

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

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

二つの変種のいずれかを選んでください:

API 21以降で利用可能
  • Switch、すべてのテーマの属性がAPPCOMPAT-V7サポートライブラリで利用可能なandroid:
  • SwitchCompat、いくつかのテーマの属性を先頭にしていないという接頭辞が付きます(あなたが知っていることを確認してください)。
0

switch stylingのstyle.xmlにこれを追加します。

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

とあなたのxml使用中の

<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

私はより良い何を記述するために「ThemeOverlay」に「スタイル」に変更ハプニング。 –

1

おそらく、あなたはあなたがスタイルテーマ一緒を混合している代わりに、style="@style/SwitchStyle"

関連する問題