gpt4 book ai didi

android - 自定义开关出现拉伸(stretch)

转载 作者:行者123 更新时间:2023-11-30 02:32:03 25 4
gpt4 key购买 nike

我已经为 Switch 的拇指和轨道开发了一个自定义可绘制对象,但问题是它看起来被拉伸(stretch)了:

enter image description here

有人知道是什么原因造成的吗?我的 XML 如下:

布局.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"/>

绿色轨道可绘制对象:

enter image description here

最佳答案

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

关于android - 自定义开关出现拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com