在冰淇淋三明治中,引入了一个Switch Widget,它显示了On Off Slider.
我像这样添加了Switch:
<Switch
android:layout_width="fill_parent"
android:layout_height="48dp"
android:textStyle="bold"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_bg_selector" />
Run Code Online (Sandbox Code Playgroud)
轨道和拇指绘图是九个补丁图像,可以扩展到所有可能的大小.我希望Switch可以扩展到给定边界内的最大大小,但似乎drawable只是在提供的空间内居中.
是否可以增加Switch的大小以使其看起来更大?
我正在尝试创建一个自定义开关按钮,其拇指约占其轨道的75%(宽度).我不想使用图像,只是可绘制的形状,样式等.到目前为止,我已经完成了以下工作:
activity_main.xml中
<Switch
android:id="@+id/onOffSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:track="@drawable/switch_custom_track"
android:thumb="@drawable/switch_custom_thumb"
android:showText="true"
android:textOff="Off"
android:textOn="On"/>
Run Code Online (Sandbox Code Playgroud)
switch_custom_track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/switch_custom_track_on" />
<item android:state_checked="false" android:drawable="@drawable/switch_custom_track_off" />
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_custom_track_on.xml
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="#00b800" />
<size android:width="90dp" android:height="40dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
switch_custom_track_off.xml
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="#fd491d" />
<size android:width="90dp" android:height="40dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
switch_custom_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/switch_custom_thumb_on" />
<item android:state_checked="false" android:drawable="@drawable/switch_custom_thumb_off" />
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_custom_thumb_on.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ffffff" />
<size android:width="70dp" …Run Code Online (Sandbox Code Playgroud) 我的Switch按钮的拇指似乎变得歪斜(开启和关闭状态).在github上也有类似的问题,但那些是人们在API 4.0中使库支持Switch按钮 -
main包含开关按钮,并且有一个拇指和轨道可绘制的应用程序
这就是发生的事情:

这就是它的假设:

switch_track_on.png

main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="112dp"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_track_selector"
android:textOn=""
android:textOff=""/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
switch_thumb_selector.xml
<selector>
<item android:drawable="@drawable/switch_thumb">
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_track_selector.xml
<selector>
<item android:drawable="@drawable/switch_track_on" android:state_checked="true"/>
<item android:drawable="@drawable/switch_track_off" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)