[英]Material 3 - Change switch icon size in style.xml
我正在嘗試通過thumbIcon
將圖標添加到switch
控件,但我找不到調整圖標大小的方法。 我正在嘗試在此頁面上了解此內容,但我無法做到。
如果我使用不帶selector
的drawable
對象,圖標顯示正確的大小,但如果我使用帶selector
的drawable
對象,圖標大小會變大,大於16dp
並且不美觀。 有沒有辦法使用可繪制模式選擇器更改圖標大小? 或者這可以通過某種方式完成嗎? 我希望當開關處於未選中位置時,它顯示的圖標與選中位置不同的圖標具有正確的尺寸或適合度。
材質開關
<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" />
風格.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>
可繪制/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>
您可以在選擇器中使用圖層列表:
<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>
我必須將重力設置為中心,否則圖標不在拇指中心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.