【问题标题】:Customized switch toggle button自定义开关切换按钮
【发布时间】:2015-03-31 12:13:04
【问题描述】:

我不想在 android 中自定义我的开关。问题是默认按钮覆盖了我自己的drawable。

我做了什么:

  1. 创建我自己的图形(2 个图形)
  2. 在drawable文件夹中创建一个xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_female" android:state_checked="false"/>
    <item android:drawable="@drawable/switch_male" android:state_checked="true"/>
</selector>
  1. 我的开关定义如下所示:
<Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textOff=""
            android:textOn=""
            android:id="@+id/genderSwitchOne"
            android:layout_below="@+id/txtViewPersonGender"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="25dp"
            android:background="@drawable/gender_switch"/>

但我得到的是:

是否可以删除默认按钮(在女性的“em”上方)?

提前致谢。

PS: 抱歉,我无法在没有块引用的情况下粘贴 android xml。

【问题讨论】:

    标签: android xml android-layout togglebutton android-togglebutton


    【解决方案1】:

    Switch XML 元素上设置android:thumb="@null"。这将从显示中删除拇指可绘制对象。

    编辑:由于上面产生了 NPE,android:thumb="@android:color/transparent" 应该可以解决问题,因为这将作为实际的 ColorDrawable 加载。

    【讨论】:

    • 也许您想制作自定义颜色的两种状态拇指,保留原始背景并实现textOntextOff,而不是将文本硬编码到背景图像中。不过我还没有测试过。
    • 根据您的建议,我得到了java.lang.NullPointerException at android.widget.Switch.jumpDrawablesToCurrentState(Switch.java:822)
    猜你喜欢
    • 2015-01-24
    • 2020-03-21
    • 2014-06-15
    • 2018-08-05
    • 2011-03-31
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多