2016-08-29 5 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:

<?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> 

おそらく誰かが同様の問題に直面しました。あなたの経験を共有してください。


EDIT:追加dimensは、私はあなたのコードをコピーしてswitch_track_widthを持っている必要があり<size> widthプロパティの内側にあなたの描画可能/ 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にあります。

以前のバージョンでは、Lollipopの前に親指がテキストとともに使用されていました。ドロアブルは、必要なサイズにスケーリングされた背景画像にすぎませんでした。これを確認するには、textOfftextOnの属性にテキストを追加します。さらに、最小限の幅が定義されています。

だから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

を使用しましたswitch_track_heightの代わりに:

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

あなたの問題は解決しますが、 RES /値-V21のディレクトリ内の1つの以上dimens.xmlファイルを示唆し、あなたはまた、RES /値/ 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

あなたが言ったような変更を加えましたが、何も起こらない...サイズのプロパティは何らかの理由で無視されます。 – AnZ

+0

私の仕事はありがたいです – Dentor

0

このウィジェット "android.support.v7.widget.switchcompat"を使用できます。これは下位互換性をサポートしています。

+0

は動作していないようです。 'SwitchCompat'に変更され、今見ているもの - http://i.imgur.com/feh8eAf.png。サイズはすべてのAPIレベルで変更されていません – AnZ

関連する問題