简体   繁体   English

Android 4中的自定义切换小部件

[英]Custom switch widget in Android 4

I am getting crazy with switch buttons in Android 4. 我对Android 4中的切换按钮感到疯狂。

First of all, I have set a selector as background with two different PNG files selected from drawable folder: 首先,我设置了一个选择器作为背景,从drawable文件夹中选择了两个不同的PNG文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/form_switch_no" android:state_checked="false"/>
    <item android:drawable="@drawable/form_switch_ok" android:state_checked="true"/>
    <item android:drawable="@drawable/form_switch_no"/>
</selector>

but I don't know why they are x-scaled. 但我不知道为什么他们是x-scaled。 I want to manage this size, but I have tried to change layout_width/height/weight... without succes. 我想管理这个大小,但我试图改变layout_width / height / weight ...没有成功。

I have also a custom PNG file to be used as the thumb of the switch. 我还有一个自定义PNG文件用作交换机的拇指。 I also need to manage the size of this thumb, because if I set textOn and textOff propieties as "", thumb gets a very small size. 我还需要管理这个拇指的大小,因为如果我将textOn和textOff propments设置为“”,拇指的大小就会非常小。 Moreover, I would like to change thumb padding in respect of the background, because It does not appears centered. 此外,我想改变背景的拇指填充,因为它不会出现居中。

Here is my Switch XML definition: 这是我的Switch XML定义:

<Switch
    android:id="@+id/switchUsers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/user"
    android:textOn=""
    android:textOff=""
    android:thumb="@drawable/form_switch_slider"
    android:track="@drawable/switch_user_selector" />

I have tried to change image sizes and making them nine-patch ones, but I can't reach my target. 我试图改变图像大小并使它们成为九个补丁,但我无法达到目标。

Any help will be appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

The solucion for the extremely small slider with no text was solved with a combination of: 没有文字的极小滑块的解决方案通过以下组合解决:

android:thumbTextPadding="25dp"
android:textOn=""
android:textOff=""

Then, the problem of the small padding in the same slider was a 9-patch image problem. 然后,同一滑块中的小填充问题是9补丁图像问题。 I had to extend bottom and right black lines one more pixel simetrically. 我不得不在底部和右边的黑色线上再增加一个像素。

Hope this helps someone with the same problem. 希望这可以帮助有同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM