2016-08-29 7 views
15

अनुरूप आकार स्विच काम नहीं कर रहा है कि जैसा दिखता है:कस्टम स्विच - ट्रैक और चयनकर्ता आकार के नीचे 21 एपीआई

एपीआई से ऊपर 21

enter image description here

एपीआई 21

enter image description here

नीचे

लगता है कि <size/> ब्लॉक पूर्व 21 एपीआई के लिए <shape/> में काम नहीं करता है।

कोई विचार यह कैसे हल करें?


कोड

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

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

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

<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 डेटा फ़ाइलों के लिए प्रारूप उपयोगकर्ता होता है जैसे कि सादे पाठ या ".ini" बिना किसी सवाल के प्रारूप में इस्तेमाल किया गया प्रारूप, वास्तव में, इसके साथ कुछ लेना देना नहीं है। – bitifet

उत्तर

7

सब कुछ <size /> टैग के साथ ठीक है। Drawable बनाया गया है और सही ढंग से लागू किया गया है। आपकी समस्या Switch के भीतर पूरी तरह से निहित है।

पुराने संस्करणों में लॉलीपॉप से ​​पहले अंगूठे को पाठ के साथ प्रयोग किया जाता था और खींचने योग्य पृष्ठभूमि छवि से अधिक कुछ नहीं था जो आवश्यक आकार के लिए बढ़ाया गया था। आप textOff और textOn विशेषताओं में टेक्स्ट जोड़कर इसे सत्यापित कर सकते हैं। इसके अतिरिक्त एक न्यूनतम चौड़ाई परिभाषित है।

तो बस एक 0 की switchMinWidth अंगूठे

<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

इस्तेमाल किया मैं अपने कोड की नकल की और मेरी मशीन में, <size> चौड़ाई संपत्ति के अंदर अपने drawable/switch_selector.xml में पहली बात को लागू करने के switch_track_width होना चाहिए की कोशिश की है switch_track_height के बजाय:

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

हालांकि आपकी समस्या का समाधान होगा, लेकिन मैं res/values-V21 निर्देशिका के अंदर एक और 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> 

आप एक भी के लिए res/values ​​/ dimens.xml चौड़ाई, ऊंचाई और त्रिज्या बदल जाते हैं।

आशा है कि इससे मदद मिलेगी।

+0

कर सकते हैं मैंने आपके जैसे परिवर्तन किए हैं लेकिन कुछ भी खुश नहीं है ... आकार की संपत्ति प्रतीत होती है किसी कारण से अनदेखा किया जा रहा है। – AnZ

+0

मेरे लिए यह काम धन्यवाद – Dentor

0

आप इस विजेट का उपयोग "android.support.v7.widget.switchcompat" कर सकते हैं। यह समर्थन पिछड़ा संगतता का समर्थन करता है।

+0

काम नहीं कर रहा प्रतीत होता है। 'स्विचकंपेट' में बदल गया और यह अब कैसा दिखता है - http://i.imgur.com/feh8eAf.png। आकार सभी एपीआई स्तरों के लिए अब नहीं बदलता – AnZ

संबंधित मुद्दे