Switch widget을 on/off 이미지를 customize하여 사용중인데

Android 5.1 기준으로 개발하여 Android 4.3에서 확인시 on/off image가 실제 이미지 보다 width가 축소되어 찌그러져 보이는 문제가 있었다.

아무리 찾아도 원인을 알 수 없던 차에 StackOverFlow에서 해결 방법을 찾았다.


http://stackoverflow.com/questions/39206494/custom-switch-track-and-selector-size-not-working-below-21-api


해결에 참고한 내용은 아래와 같다.



Everything is ok with the <size /> tag. The Drawable is created and applied correctly. Your issue lies completely within the Switch.

In older versions the before Lollipop the thumb was used with text and the drawable was nothing more than a background image which got scaled to the size necessary. You can verify this by adding text to the textOff and textOn attributes. Additionally there's a minimal width defined.

So just add a switchMinWidth of 0 and a thumbTextPadding of half the diameter of the thumb

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="0dp"
    android:textOff=""
    android:textOn=""
    android:thumb="@drawable/switch_selector"
    android:thumbTextPadding="@dimen/switch_thumb_radius"
    android:track="@drawable/switch_track" />

and a correct radius definition for it

<dimen name="switch_track_height">30dp</dimen>
<dimen name="switch_thumb_radius">15dp</dimen>



문제가 해결된 소스


styles.xml
<style name="switchStyle">
    <item name="android:thumb">@drawable/switch_button</item>
    <item name="android:layout_width">72dp</item>
    <item name="android:layout_height">30dp</item>
    <item name="android:switchMinWidth">0dp</item>
    <item name="android:thumbTextPadding">18dp</item>
    <item name="android:track">@drawable/onoffbutton_bg</item>
    <item name="android:textColor">@color/switch_text_selector</item>
    <item name="android:textOn">""</item>
    <item name="android:textOff">""</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/app_text_size_4</item>
    <item name="android:gravity">center_vertical</item>
</style>

layout.xml
<Switch
    android:id="@+id/device_settings_lost_mobile_alarm_switch"
    style="@style/switchStyle"/>
 


+ Recent posts