简体   繁体   English

材料 3 - 在 style.xml 中更改开关图标大小

[英]Material 3 - Change switch icon size in style.xml

I'm trying to add an icon to a switch control via thumbIcon , but I can't find a way to resize the icon.我正在尝试通过thumbIcon将图标添加到switch控件,但我找不到调整图标大小的方法。 I'm trying to learn about this on this Page but I haven't been able to.我正在尝试在此页面上了解此内容,但我无法做到。

If I use the drawable without a selector , the icon displays the correct size, but if I use the drawable with the selector , the icon size becomes large, larger than 16dp and unsightly.如果我使用不带selectordrawable对象,图标显示正确的大小,但如果我使用带selectordrawable对象,图标大小会变大,大于16dp并且不美观。 Is there a way to change the icon size with the drawable mode selector?有没有办法使用可绘制模式选择器更改图标大小? or can this be done in some way?或者这可以通过某种方式完成吗? I want when the switch is in the unchecked position it shows a different icon than the checked position with the right size or fit.我希望当开关处于未选中位置时,它显示的图标与选中位置不同的图标具有正确的尺寸或适合度。

Material Switch材质开关

<com.google.android.material.materialswitch.MaterialSwitch
    android:id="@+id/SFilter_WorldWideValue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:checked="true"
    android:fontFamily="@font/roboto_medium"
    android:text="@string/search"
    android:textAllCaps="true"
    android:textColor="@color/Text_Base_White"
    android:textIsSelectable="false"
    android:textSize="16sp"
    app:thumbIcon="@drawable/icon_check" --> **Normal Size**
    app:thumbIcon="@drawable/default_switch_icon" --> **Size is not normal**
    tools:ignore="TouchTargetSizeCheck" />

style.xml风格.xml

<style name="Theme.Default" parent="Theme.Material3.Light.NoActionBar">
    ...
    <item name="materialSwitchStyle">@style/Switch.Default</item>
</style>
    
<style name="Switch.Default" parent="Widget.Material3.CompoundButton.MaterialSwitch">
    <item name="thumbIcon">@drawable/default_switch_icon</item>
    <item name="checkedIconSize">16dp</item> // Not working
    <item name="selectorSize">16dp</item> // Not working
    <item name="iconSize">16dp</item> // Not working
    <item name="drawableSize">16dp</item> // Not working
    <item name="itemIconSize">16dp</item> // Not working
    <item name="maxImageSize">16dp</item> // Not working
    <item name="materialThemeOverlay">@style/Switch.Colors.Default</item>
</style>

<style name="Switch.Colors.Default" parent="">
    ...
    ...
</style>

drawable/default_switch_icon.xml可绘制/default_switch_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:state_checked="true"
        android:drawable="@drawable/icon_check"/>
    <item
        android:state_checked="false"
        android:drawable="@drawable/icon_close"/>
</selector>

You can use a layer-list in your selector:您可以在选择器中使用图层列表:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- State Checked -->
    <item android:state_checked="true">
        <layer-list>
            <item android:drawable="@drawable/ic_check"
                android:height="16dp"
                android:width="16dp"
                android:gravity="center"/>
        </layer-list>
    </item>

    <!-- State Unchecked -->
    <item android:drawable="@color/transparent" />
</selector>

I had to set the gravity to center or the icon was not centered in the thumb.我必须将重力设置为中心,否则图标不在拇指中心。

thumb_with_specific_size_icon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM