【问题标题】:Custom switch widget in Android 4Android 4 中的自定义开关小部件
【发布时间】:2013-03-21 16:54:28
【问题描述】:

我对 Android 4 中的开关按钮感到疯狂。

首先,我将选择器设置为背景,其中包含从可绘制文件夹中选择的两个不同的 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>

但我不知道为什么它们是 x 缩放的。我想管理这个尺寸,但我试图改变 layout_width/height/weight... 没有成功。

我还有一个自定义 PNG 文件用作开关的拇指。我还需要管理这个拇指的大小,因为如果我将 textOn 和 textOff 属性设置为“”,拇指会变得非常小。此外,我想更改背景的拇指填充,因为它没有出现居中。

这是我的 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" />

我已尝试更改图像大小并将它们设为九个补丁,但我无法达到我的目标。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android android-4.0-ice-cream-sandwich


    【解决方案1】:

    没有文本的极小滑块的解决方案是通过以下组合解决的:

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

    然后,同一个滑块中的小填充问题是一个 9-patch 图像问题。我不得不将底部和右侧的黑色线条等距延长一个像素。

    希望这可以帮助遇到同样问题的人。

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多