Java 如何更改 Android Switch 轨道的宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16599327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Width of Android's Switch track?
提问by NeilDA
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. 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. 'minWidth' doesnt seem to do anything.
我快被这个小问题搞疯了。我有一个“切换”表单小部件,但无论我尝试多少次,都无法将其缩小。即使我有一个字符而不是“ON”或“OFF”,开关的大小也保持不变。“拇指”变小,但必须像以前一样拖动它。将 'layout_width' 更改为较小的值只会切断剩余的轨道。'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=" " />
<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:
但我想要这样的东西:
采纳答案by BSKANIA
set your desired width of switch in this attribute.
在此属性中设置所需的开关宽度。
android:switchMinWidth
android:switchMinWidth
like
喜欢
<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" />
Thanks, bskania
谢谢,斯卡尼亚
回答by Pedro Acácio
Here is my solution. I removed the texts, set the padding of the track and define the switchMinWidth prop. This is my xml:
这是我的解决方案。我删除了文本,设置了轨道的填充并定义了 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" />
Hope it helps somebody.
希望它可以帮助某人。
回答by benli shi
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" />