【问题标题】:Default Switch overlapping with Custom Switch默认开关与自定义开关重叠
【发布时间】:2015-10-28 12:24:32
【问题描述】:

我正在尝试使用两个图像分别表示 false 和 true 来实现自定义开关。但问题是我们的默认开关图像与自定义开关图像重叠。

下面是我的 Switch 代码:

<Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you want the current offer as your final offer"
        android:background="@null"
        android:button="@null"
        android:textOff=""
        android:textOn=""
        android:drawableRight="@drawable/switch"
        android:textSize="16sp"
        android:textColor="#9B9B9B"
        android:id="@+id/switch1"
         />

以下是我的switch.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item
       android:drawable="@drawable/dummy_toggle_off"
       android:state_checked="false" />
   <item
       android:drawable="@drawable/dummy_toggle_on"
       android:state_checked="true" />
   <item
       android:drawable="@drawable/dummy_toggle_off"/>

</selector>

这是生成的图像:

【问题讨论】:

    标签: android button toggle


    【解决方案1】:

    使用以下代码进行开关定制-

    <Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:thumb="@drawable/switch_thumb"
    android:track="@drawable/switch_track" />
    

    这里switch_thumb 是您的thumb 的样式 xml 文件,switch_track 是您的开关background 的xml 样式。 更多信息请参考链接-link

    switch_thumb.xml

    <selector>
    <item android:drawable="@drawable/your_png_for_thumb">
    </item>
    </selector>
    

    switch_track.xml

    <selector>
      <item android:drawable="@drawable/your_png_for_track_grey"      
          android:state_checked="true"/>
      <item android:drawable="@drawable/your_png_for_track_blue" 
          android:state_checked="false"/>
    </selector>
    

    你可以从网上下载这些png图片。下面是一个链接,你可以在其中找到那些png-icon finder

    【讨论】:

    • 我的问题是如何删除与自定义图像重叠的默认开关图像!我不知道你的答案如何适合这个问题。
    • 我想改变开关的整体形状..不仅仅是拇指形状!!所以,我认为你的答案不是这个问题的必填答案。
    • 查看此链接以获取帮助-customize switch
    • 谢谢..它解决了我的问题。你能用你在评论中提到的链接编辑答案吗?所以,我可以接受这个答案!谢谢
    • 好的,我会试试的。如果您愿意,请为答案投票,以便其他人可以从中受益。谢谢。
    【解决方案2】:

    我有同样的问题,我认为这个解决方案。

    在您的 XML 开关中添加此代码。

    android:thumb="@android:color/transparent"
    android:track="@android:color/transparent"
    android:background="@drawable/yourSelector"
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2019-02-28
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多