2016-09-13 4 views
0

나는 안드로이드 5 및 6에서 멋지게 보이는 사용자 정의 스위치 요소를 가지고 있습니다. 그러나 안드로이드 4에는 문제가 있습니다. 질문은 토글 원형의 모양을 유지하는 방법입니까?안드로이드 4에서 사용자 정의 스위치 모양을 수정하는 방법

Android 5,6 사진 1

enter image description here

안드로이드 4. 사진 여기에 2

enter image description here

내 코드입니다 :

<Switch 
      android:id="@+id/row_device_switch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:textOn="ON" 
      android:thumb="@drawable/customswitchselector" 
      android:track="@drawable/custom_track" 
      android:textOff="OFF" 
      android:layout_marginRight="14dp" 
      android:switchMinWidth="@dimen/custom_switcher_track_width" 
      /> 

custom_track.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape 
      android:shape="rectangle" 
      android:visible="true" 
      android:dither="true" 
      android:useLevel="false" 
      > 
      <corners 
       android:radius="@dimen/cutsom_switcher_track_radius"/> 
      <size 
       android:width="@dimen/custom_switcher_track_width" 
       android:height="@dimen/custom_switcher_track_height" /> 
      <stroke 
       android:width="@dimen/custom_switcher_stroke_thickness" 
       android:color="@android:color/white" 
       /> 
     </shape> 
    </item> 
    <item 
     android:left="@dimen/custom_switcher_track_line_horizontal_margin" 
     android:right="@dimen/custom_switcher_track_line_horizontal_margin" 
     > 
     <shape 
      android:shape="line" 
      > 
      <stroke 
       android:width="@dimen/custom_switcher_екфсл_thickness" 
       android:color="@android:color/white" 
       /> 
     </shape> 
    </item> 
</layer-list> 
,

customswitchselector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_checked="true"> 
     <layer-list> 
      <item> 
       <shape 
        android:shape="oval" 
        android:visible="true" 
        android:dither="true" 
        android:useLevel="false" 
        > 
        <solid 
         android:color="@color/colorListEnd" 
         /> 
        <size 
         android:width="@dimen/custom_switcher_circle_size" 
         android:height="@dimen/custom_switcher_circle_size" /> 
        <stroke 
         android:width="@dimen/custom_switcher_stroke_thickness" 
         android:color="@android:color/white"/> 
       </shape> 
      </item> 
      <item 
       android:left="@dimen/custom_switcher_between_circles_margin" 
       android:right="@dimen/custom_switcher_between_circles_margin" 
       android:top="@dimen/custom_switcher_between_circles_margin" 
       android:bottom="@dimen/custom_switcher_between_circles_margin" 
       > 
       <shape 
        android:shape="oval" 
        > 
        <size 
         android:width="@dimen/custom_switcher_inner_circle_size" 
         android:height="@dimen/custom_switcher_inner_circle_size" /> 
        <solid 
         android:color="@android:color/white"/> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 
    <item android:state_checked="false"> 
     <layer-list> 
      <item> 
       <shape 
        android:shape="oval" 
        android:visible="true" 
        android:dither="true" 
        android:useLevel="false"> 

        <solid 
         android:color="@color/colorListEnd" 
         /> 
        <size 
         android:width="@dimen/custom_switcher_circle_size" 
         android:height="@dimen/custom_switcher_circle_size" /> 
        <stroke 
         android:width="@dimen/custom_switcher_stroke_thickness" 
         android:color="@android:color/white"/> 
       </shape> 
      </item> 
      <item 
       android:left="@dimen/custom_switcher_between_circles_margin" 
       android:right="@dimen/custom_switcher_between_circles_margin" 
       android:top="@dimen/custom_switcher_between_circles_margin" 
       android:bottom="@dimen/custom_switcher_between_circles_margin" 
       > 
       <shape 
        android:shape="oval"> 
        <size 
         android:width="@dimen/custom_switcher_inner_circle_size" 
         android:height="@dimen/custom_switcher_inner_circle_size" 
         /> 
        <stroke 
         android:width="@dimen/custom_switcher_stroke_thickness" 
         android:color="@android:color/white" 
         /> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 
</selector> 

답변

1

Switch가 균일하게 작동하지 만드는 여러 작은 문제가 있습니다. 귀하의 경우 android:textOffandroid:textOn 값을 ""으로 사용해야합니다. 그렇지 않으면 트랙과 엄지 너비가 텍스트를 고정하도록 조정됩니다. 또한 엄지의 크기를 확인하려면 android:thumbTextPadding을 정의해야합니다. 드로어 블은 배경에 불과하며 직접 적용되지 않습니다.

<Switch 
    android:id="@+id/row_device_switch" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:layout_marginRight="14dp" 
    android:switchMinWidth="@dimen/custom_switcher_circle_radius" 
    android:textOff="" 
    android:textOn="" 
    android:thumb="@drawable/customswitchselector" 
    android:thumbTextPadding="@dimen/switch_thumb_radius" 
    android:track="@drawable/custom_track" /> 

@dimen/custom_switcher_circle_radius@dimen/custom_switcher_circle_size 치수의 절반의 값을 가져야한다.

+0

당신은 최고입니다! 고맙습니다! – user3400881

관련 문제