【问题标题】:How to change Width of Android's Switch track?如何更改 Android 的 Switch 轨道的宽度?
【发布时间】:2013-05-16 23:53:58
【问题描述】:

这个小问题我快疯了。我有一个“开关”表单小部件,但无论我尝试多少,我都无法使其更窄。即使我有一个字符而不是“ON”或“OFF”,开关的大小也保持不变。 “拇指”变小了,但它必须像以前一样拖过相同的距离。将“layout_width”更改为较小的值只会切断剩余的轨道。 'minWidth' 似乎没有做任何事情。

有人知道我该怎么做吗?理想情况下,我只想要一个空拇指,我会对两个拇指进行颜色编码以知道哪个是哪个。

XML 代码:

<Switch
     android:id="@+id/switch3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Switch"
     android:textOff=" "
     android:textOn=" " />

我知道了:

但我想要这样的东西:

【问题讨论】:

  • 向我们展示您的 xml 代码
  • android:layout_width=""
  • 然后将左侧修剪掉。双方的差距还是一样的。我希望两者之间的差距更窄甚至重叠。
  • 剪掉了?这很奇怪。缩小你的父容器,看看会发生什么,或者在你的父容器中插入填充。如果您发布您关心的 XML,将会很有帮助。
  • 我什至检查了一个空白活动,其相对布局仅包含上面的开关小部件。不过,它正在被修剪掉。双方之间的这种顽固差距似乎并没有变窄。 :(

标签: java android


【解决方案1】:

在属性中设置你想要的开关宽度:

android:switchMinWidth

例如:

<Switch
    android:id="@+id/switchVisitAgain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_margin="1dp"
    android:checked="false"
    android:gravity="center_vertical"
    android:switchMinWidth="56dp"
    android:textOff=""
    android:textOn=""
    android:thumb="@drawable/round_button"
    android:track="@drawable/button_black" />

【讨论】:

  • 这会修剪开关的左侧部分。关于如何实现对称开关有什么建议吗?
  • 是的,如果您使用默认开关,那么它只会修剪右侧部分。我已经将此解决方案用于开关的自定义可绘制对象。
  • 花了将近两周的时间寻找解决方案,一直在尝试“setminWidth”,您就是救命稻草! PS: switchMinWidth="0dp" 是我使用的。这样开关会根据可绘制对象进行缩放
  • 无法正确设置小于 2x thumbs 的宽度~stackoverflow.com/questions/20762490/…
【解决方案2】:

这是我的解决方案。我删除了文本,设置了轨道的填充并定义了switchMinWidth 属性。这是我的 xml:

<Switch
    android:id="@+id/theSwitchId"
    android:textOn=""
    android:textOff=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:thumbTextPadding="5dp"
    android:switchMinWidth="40dp"
    android:layout_gravity="right|center_vertical" />

【讨论】:

    【解决方案3】:

    谁在使用androidx.appcompat.widget.SwitchCompat需要使用

    app:switchMinWidth
    

    例子:

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:switchMinWidth="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:track="@drawable/custom_track"
        android:thumb="@drawable/custom_thumb"
       />
    

    【讨论】:

      【解决方案4】:

      您应该使用android:track 而不是android:thumb

      我所有的代码:

      <Switch
          android:id="@+id/switch_security_tog"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:layout_marginRight="16dp"
          android:background="@color/transparent"
          android:button="@null"
          android:textOff=""
          android:textOn=""
          android:thumb="@null"
          android:switchMinWidth="56dp"
          android:track="@drawable/thumb" />
      

      【讨论】:

        猜你喜欢
        • 2014-01-12
        • 1970-01-01
        • 2012-07-26
        • 1970-01-01
        • 2015-03-06
        • 1970-01-01
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多