kotlin 更改Material 3开关的轮廓颜色

gzszwxb4  于 2023-11-21  发布在  Kotlin
关注(0)|答案(2)|浏览(155)

我试图找到一种方法来改变一个材料3开关的轮廓颜色,由于一些奇怪的原因,我找不到一种方法,我会很感激,如果有人能告诉我如何在XML和代码背后做到这一点。
此外,如果您能指导我了解Material 3组件的文档,我将不胜感激!
我当前的switch看起来像这样:

<com.google.android.material.materialswitch.MaterialSwitch
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:text="Material 3"/>

字符串
多谢帮忙!

unhi4e5o

unhi4e5o1#

根据材料组件的文件:

1.定义新的MaterialSwitch样式

styles.xml中定义新样式,定义materialThemeOverlay

<style name="Your.Style.Name" parent="Widget.Material3.CompoundButton.MaterialSwitch">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Switch</item>
</style>

字符串

2.定义您的ThemeOverlay

<style name="ThemeOverlay.App.Switch" parent="">
    <item name="colorPrimary">@color/pink_200</item>
    <item name="colorSurfaceVariant">@color/pink_100</item>
</style>


在这里,你有两种方法:将你的样式实现到allMaterialSwitch或手动将你的样式应用到你的MaterialSwitch

3.将样式应用于所有MaterialSwitch

在styles.xml中的主样式中,为所有MaterialSwitch应用新样式

<style name="Theme.App" parent="Theme.Material3.*">
    ...
    <item name="materialSwitchStyle">@style/Widget.App.Switch</item>
</style>

3.1申请单例MaterialSwitch样式

<com.google.android.material.materialswitch.MaterialSwitch
        ...
    style="@style/Widget.App.Switch"
    />

8wtpewkr

8wtpewkr2#

1.使用XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="32dp"
    android:orientation="vertical">

    <com.google.android.material.materialswitch.MaterialSwitch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        app:trackDecorationTint="#AA3710" />

    <com.google.android.material.materialswitch.MaterialSwitch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        app:trackDecorationTint="#AA3710" />

</androidx.appcompat.widget.LinearLayoutCompat>

字符串
在这里,您需要使用app:trackDecorationTint="#AA3710"设置轨道轮廓颜色,并将#AA3710设置为您想要的任何颜色。如果您像上面的示例中那样提供单一颜色,则相同的颜色将用于所有状态,如选中,未选中,禁用。要为每个状态提供单独的颜色,您可以将引用传递到颜色的状态列表XML文件。
输出


的数据
1.使用代码(Java)
为每个状态单独设置颜色

int[][] states = new int[][]{
        new int[]{android.R.attr.state_enabled}, // enabled
        new int[]{-android.R.attr.state_enabled}, // disabled
        new int[]{-android.R.attr.state_checked}, // unchecked
        new int[]{android.R.attr.state_pressed}  // pressed
};
int[] colors = new int[]{
        Color.BLACK,
        Color.RED,
        Color.GREEN,
        Color.BLUE
};
ColorStateList colorStateList = new ColorStateList(states, colors);
switchMaterial.setTrackDecorationTintList(colorStateList);


所有州的颜色相同

ColorStateList colorStateList = ColorStateList.valueOf(Color.RED);
switchMaterial.setTrackDecorationTintList(singleColorcolorStateListStateList);

相关问题