简体   繁体   English

如何更改 Android Switch 轨道的宽度?

[英]How to change Width of Android's Switch track?

I'm going crazy with this tiny problem.我快被这个小问题搞疯了。 I have a 'switch' Form widget, but no matter how much I try, I cannot make it narrower.我有一个“切换”表单小部件,但无论我尝试多少次,都无法将其缩小。 Even if I have one character instead of 'ON' or 'OFF', the size of switch remains the same.即使我有一个字符而不是“ON”或“OFF”,开关的大小也保持不变。 The 'thumb' becomes small, but it has to be dragged over the same distance as before. “拇指”变小,但必须像以前一样拖动它。 Changing the 'layout_width' to a smaller value simply cuts off the remaining track.将 'layout_width' 更改为较小的值只会切断剩余的轨道。 'minWidth' doesnt seem to do anything. 'minWidth' 似乎没有做任何事情。

Anybody knows how I can do this?有谁知道我怎么能做到这一点? Ideally I want just an empty thumb and I'll colour code both thumb to know which is which.理想情况下,我只想要一个空拇指,我会对两个拇指进行颜色编码,以知道哪个是哪个。

XML code: XML 代码:

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

I am gettin this:我开始这个:在此处输入图片说明

but I want something like this:但我想要这样的东西:在此处输入图片说明

Set your desired width of switch in the attribute:在属性中设置所需的开关宽度:

android:switchMinWidth

for example:例如:

<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" />

Here is my solution.这是我的解决方案。 I removed the texts, set the padding of the track and define the switchMinWidth properties.我删除了文本,设置了轨道的填充并定义了switchMinWidth属性。 This is my xml:这是我的 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" />

Who are using androidx.appcompat.widget.SwitchCompat need to use使用androidx.appcompat.widget.SwitchCompat需要使用

app:switchMinWidth

Example:例子:

<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"
   />

You should use android:track instead of android:thumb .您应该使用android:track而不是android:thumb

All my code:我所有的代码:

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM