簡體   English   中英

"SwitchCompat 改變顏色"

[英]SwitchCompat change color

我需要更改 SwitchCompat 的軌道顏色。 我試過這個,但它對我不起作用。 這是我的 XML 文件的代碼

<android.support.v7.widget.SwitchCompat
    android:id="@+id/sc_push"
    style="@style/switchStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:theme="@style/switchStyle"
    app:theme="@style/switchStyle" />

這是我的 style.xml 文件

<style name="switchStyle">
    <item name="colorControlActivated">@color/red</item>
    <item name="android:colorForeground">@color/gray</item>
</style>

似乎有什么問題?

此外,我無法更改活動的顏色或基礎應用程序的顏色。 我必須改變這個單一視圖的顏色。

試試這個代碼。

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       ...
       <!-- Active thumb color & Active track color(30% transparency) -->
       <item name="colorControlActivated">@color/theme</item>
       <!-- Inactive thumb color -->
       <item name="colorSwitchThumbNormal">@color/grey300</item>
       <!-- Inactive track color(30% transparency) -->
       <item name="android:colorForeground">@color/grey600</item>
       ...
    </style>

更好的方法是,

  1. 使用選擇器標簽創建如下的新資源文件 switch_track_color.xml。
<item android:color="#15A215" android:state_checked="true"/>
<item android:color="#BAC7CB" android:state_checked="false"/>
2. 將此樣式用於 SwitchCompat。
 <style name="SwitchStyle"> <item name="thumbTint">#ffffff</item> <item name="trackTint">@color/switch_track_color</item> </style>

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM