![](/img/trans.png)
[英]How to reduce material.switchmaterial.SwitchMaterial end margin in android?
[英]How to style google SwitchMaterial button on Android?
我有一個這樣的材料切換按鈕:
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/effectEnabled"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
style="@style/ButtonStyle2"
android:checked="true"
android:enabled="true" />
這是風格:
<style name="ButtonStyle2" parent="Base.Widget.AppCompat.Button">
<!--<item name="android:fontFamily">sans-serif-light</item>-->
<!--<item name="android:textStyle">bold</item>-->
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:textSize">20sp</item>
<item name="android:textAllCaps">false</item>
</style>
使用這些 styles,按鈕會損壞並帶有灰色方形背景。
如何設置 SwitchMaterial 按鈕的樣式? 我只想改變開關顏色
您可以使用app:thumbTint
和/或app:trackTint
更改開關 colors。 為了獲得更好的結果,在res/color
中定義一個ColorStateList
(即: res/color/custom_thumb_selector.xml
)以根據是否選中而具有不同的 colors。 IE:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/customChecked" />
<item android:color="@color/customUnchecked" />
</selector>
然后將其設置為您的樣式:
<style name="ButtonStyle2" parent="Widget.MaterialComponents.CompoundButton.Switch">
<item name="thumbTint">@color/custom_thumb_selector</item>
</style>
注意:您的自定義樣式應繼承自Widget.MaterialComponents.CompoundButton.Switch
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.