自定义开关出现拉伸

时间:2014-11-29 12:58:22

标签: android

我为Switch的拇指和音轨开发了一个自定义绘图,但问题是它出现了拉伸状态:

enter image description here

有没有人知道造成这种情况的原因是什么? 我的XML如下:

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toLeftOf="@+id/switch1"
        android:padding="5dp" />

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:thumb="@drawable/switch_thumb_selector"
        android:track="@drawable/switch_track_selector" />

    <TextView
        android:id="@+id/switchErrorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/switch1"
        android:layout_centerHorizontal="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/holo_red_light"
        android:visibility="gone" />

</RelativeLayout>

switch_thumb_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/switch_thumb_inactive" android:state_pressed="true"/>
    <item android:drawable="@drawable/switch_thumb_inactive" android:state_checked="true"/>
    <item android:drawable="@drawable/switch_thumb_inactive"/>

</selector>

switch_track_selector.xml

<item android:drawable="@drawable/switch_track_green" android:state_checked="true"/>
<item android:drawable="@drawable/switch_track_white" android:state_checked="false"/>
<item android:drawable="@drawable/switch_track_white"/>

Green Track Drawable:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为这种不良行为已经出现,因为你没有画出“内容”区域。尝试这个drawable,我已经测试过,它对我有用。 switch_bg.9.png

相关问题