2016-08-29 4 views
15

사용자 정의 모양 스위치 아래에 작동하지 트랙과 선택의 크기는 다음과 같습니다사용자 정의 스위치 - (21) API

API 위의 API (21)

enter image description here

아래 21

enter image description here

<size/> 블록이 pre21 API에 대해 <shape/>에서 작동하지 않는 것 같습니다.

어떤 아이디어가이 문제를 해결할 수 있습니까?


CODE

container.xml :

<Switch 
     android:id="@id/switch_follow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:textOff="" 
     android:textOn="" 
     android:thumb="@drawable/switch_selector" 
     android:track="@drawable/switch_track"/> 

그리기/switch_selector.xml :

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <layer-list> 
     <item 
      android:bottom="@dimen/switch_selector_padding" 
      android:left="@dimen/switch_selector_padding" 
      android:right="@dimen/switch_selector_padding" 
      android:top="@dimen/switch_selector_padding"> 
      <shape 
       android:dither="true" 
       android:shape="oval" 
       android:useLevel="false" 
       android:visible="true"> 
       <gradient 
        android:angle="270" 
        android:endColor="@color/primary_white" 
        android:startColor="@color/primary_white"/> 
       <corners 
        android:radius="@dimen/switch_radius"/> 
       <size 
        android:width="@dimen/switch_track_height" 
        android:height="@dimen/switch_track_height" /> 
      </shape> 
     </item> 

    </layer-list> 
</item> 
</selector> 

그리기/switch_track.xml :

01,235,
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:dither="true" 
android:shape="rectangle" 
android:useLevel="false" 
android:visible="true"> 
<gradient 
    android:angle="270" 
    android:endColor="@color/primary_yellow_dark_v2" 
    android:startColor="@color/primary_yellow_dark_v2"/> 
<corners android:radius="@dimen/switch_radius" /> 
<stroke 
    android:width="@dimen/switch_stroke_height" 
    android:color="@android:color/transparent"> 
</stroke> 
<size 
    android:width="@dimen/switch_track_width" 
    android:height="@dimen/switch_track_height" /> 
</shape> 

아마도 누군가 비슷한 문제에 직면했을 것입니다. 귀하의 경험을 공유하십시오.


편집 : 추가 dimens 내가 코드를 복사 switch_track_width을 가져야한다 <size> 폭 건물 내부에 드로어 블/사진 switch_selector.xml에, 내 컴퓨터에서 먼저 구현하려고 한

<dimen name="switch_track_width">36dp</dimen> 
<dimen name="switch_track_height">30dp</dimen> 
<dimen name="switch_radius">50dp</dimen> 
<dimen name="switch_selector_padding">2dp</dimen> 
<dimen name="switch_stroke_height">0dp</dimen> 
+0

게시하시기 바랍니다 @ DIMEN/switch_track_width와 높이 크기에 대한 정확한 반경 정의의 절반 직경의 thumbTextPadding . – Warpzit

+0

@Warpzit, 내 편집을 확인하십시오 – AnZ

+0

XML 태그가 잘못되었다고 생각합니다 (확실하지는 않습니다). XML 구문 오류로 인해 발생하는 경우를 제외하고는 (이전 버전에서 작동하는 경우 특별히 보이지만 불가능하지는 않습니다 ...) XML은 일반 텍스트 또는 ".ini"처럼 데이터 파일의 형식 사용자로 사용됩니다 사실, 그것과 관련이없는 너무 많은 질문에 사용 된 형식. – bitifet

답변

7

<size /> 태그로 모든 것이 정상입니다. Drawable이 만들어지고 올바르게 적용됩니다. 문제가 완전히 Switch 내에 있습니다.

이전 버전에서는 롤리팝 이전에 엄지 손가락이 텍스트와 함께 사용되었으며 드로어 블은 필요한 크기로 조정 된 배경 이미지 이상이었습니다. textOfftextOn 속성에 텍스트를 추가하여이를 확인할 수 있습니다. 또한 정의 된 최소 너비가 있습니다.

그래서 단지 switchMinWidth 0의 추가와 엄지 손가락

<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" /> 

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

감사합니다. 내 문제를 해결 한 것 같아. – AnZ

1

을 사용 switch_track_height 대신 :

<size 
    android:width="@dimen/switch_track_width" 
    android:height="@dimen/switch_track_height" /> 

하지만 문제는 해결되지만 고해상도/값-V21 디렉토리 안에 또 하나 개의 dimens.xml 파일을 제안하고 당신은 또한 고해상도/값/dimens.xml의 폭, 높이 및 반경을 조정할

<dimen name="switch_track_width">30dp</dimen> //change as per your view appreance 
<dimen name="switch_track_height">25dp</dimen> //change as per your view appreance 
<dimen name="switch_radius">50dp</dimen> //change as per your view appreance 
<dimen name="switch_selector_padding">2dp</dimen> 
<dimen name="switch_stroke_height">0dp</dimen> 

추가 할 수 있습니다.

희망이 있으면 도움이 될 것입니다.

+0

언급 한 내용을 변경했지만 아무 것도 할 수 없습니다 ... size 속성은있는 것 같습니다. 어떤 이유로 무시되고있다. – AnZ

+0

감사합니다. – Dentor

0

이 위젯 "android.support.v7.widget.switchcompat"를 사용할 수 있습니다. 이전 버전과의 호환성을 지원합니다.

+0

이 (가) 작동하지 않는 것 같습니다. 'SwitchCompat'와 어떻게 바뀌 었는지 - http://i.imgur.com/feh8eAf.png. 크기가 모든 API 수준에서 변경되지 않습니다. – AnZ