【问题标题】:how to add a rounded rectangular toggle_switch in android?如何在android中添加一个圆角矩形toggle_switch?
【发布时间】:2015-04-26 01:34:02
【问题描述】:

我需要在 android 中制作一个圆角矩形切换开关,如下所示:

谁能指导我完成此操作的完整步骤。

【问题讨论】:

  • I have to have '3 lines' on the switch button and a 'tick mark' in place of ON and nothing (blank) in place of 'OFF' 如果你展示一张图片正是想要什么...
  • 你想要这样的东西:stackoverflow.com/a/21434374/4224337 ?
  • 你能分享一下你用来实现目标的方法吗?公认的解决方案不是很有帮助。具体来说,您使用的是image drawable 还是xml drawable。当开关打开时,您使用什么状态进行跟踪。
  • @VinayakGarg ...刚刚发布了解决方案..你可以看看

标签: android


【解决方案1】:

我解决了我的问题如下:

在我的 xml 布局文件中添加了一个切换按钮:

<ToggleButton
                android:id="@+id/ToggleButton1"
                android:layout_width="120dp"
                android:layout_height="25dp"
                android:layout_marginRight="30dp"
                android:layout_weight="2"
                android:background="@drawable/toogle_switch"
                android:text="ToggleButton"
                android:textOff=""
                android:textOn="" />

然后在“drawable”文件夹中定义了一个自定义的切换按钮背景“toogle_switch”,如下所示:

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

    <item android:drawable="@drawable/switchon" android:state_checked="true"></item>
    <item android:drawable="@drawable/switchoff" android:state_checked="false"></item>

</selector>

switchon 和 switchoff 是我展示的 2 张图片。

希望对大家有帮助! :)

【讨论】:

    【解决方案2】:

    给你:

    http://developer.android.com/guide/topics/ui/controls/togglebutton.html

    显示的确切图像由所谓的“选择器”或“状态列表”确定,它是将按钮状态映射到图像的一段 XML。

    http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

    你需要:

    1. 为按钮的所有可能状态(切换、按下等)准备图像并将它们放置在可绘制文件夹中
    2. 编写一个状态列表(选择器),将图像与按钮状态绑定
    3. 将此状态列表连接到按钮android:background 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 2018-10-03
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2019-06-12
      相关资源
      最近更新 更多