如何在Android中更改Switch的高度和宽度

11
  1. 我想将开关的宽度调整为120dp,高度为30dp。
  2. 同时,我不想在拇指上显示任何文本,但希望拇指覆盖总宽度的一半,因此我希望它的宽度为60dp。
  3. 另外,我希望拇指的高度比开关的高度略小,以便从三个方向可以看到背景。

对于#3,我不知道该怎么做,对于#1和#2,我尝试了以下XML代码,但没有效果:

 <Switch
         android:id="@+id/plug_switch"
         android:layout_width="120dip"
         android:layout_height="10dp"
         android:maxHeight="10dp"
         android:layout_below="@id/plug_graph_layout"
         android:layout_centerHorizontal="true"
         android:minWidth="100dp"
         android:thumb="@drawable/plugs_button"
         android:track="@drawable/btntoggle_selector"
         android:textOn=""
        android:textOff=""
         android:width="120dip" />

有人能帮我处理#1、#2和#3吗?

谢谢


https://dev59.com/U2kw5IYBdhLWcg3wDWPL - Shirish Herwade
3个回答

22

添加这个android:switchMinWidth="56dp",然后尝试一下

<Switch
         android:id="@+id/plug_switch"
         android:layout_width="120dip"
         android:layout_height="10dp"
         android:maxHeight="10dp"
         android:thumbTextPadding="25dp"
         android:switchMinWidth="56dp"
         android:layout_below="@id/plug_graph_layout"
         android:layout_centerHorizontal="true"
         android:minWidth="100dp"

1
但这只是确保minWidth不会低于56dp。我希望它能达到120dp。那么这有什么用呢? - Sunny
1
我已经做了,但问题是我的图片大于120 dp,高度大于10 dp。但我想将其固定在120和10上。 - Sunny
1
有什么进展吗?我也遇到了同样的问题。 - erik
1
无法将宽度设置为小于 2 倍拇指的大小。 - Zhming

7
这里是一个小例子,展示如何更改你的开关宽度,希望能对某些人有所帮助。
1) 使用你的颜色来设置拇指(color_thumb.xml)。
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">


<size android:height="40dp"  />
<gradient android:height="40dp" android:startColor="#FF569BDA" android:endColor="#FF569BDA"/>

2)轨道的灰色颜色(gray_track.xml)

shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"
>

<size android:height="40dp"  />
<gradient android:height="40dp" android:startColor="#dadadada" android:endColor="#dadadada"/>

3) 缩略图选择器(thumb.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/gray_track" />
<item android:state_pressed="true"  android:drawable="@drawable/color_thumb" />
<item android:state_checked="true"  android:drawable="@drawable/color_thumb" />
<item                               android:drawable="@drawable/gray_track" />

4) 轨道选择器(track.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"  android:drawable="@drawable/color_thumb" />
<item                               android:drawable="@drawable/gray_track" />

最后,在switch语句中使用。
使用:
     android:switchMinWidth="56dp"
     android:thumb="@drawable/thumb"
    android:track="@drawable/track"

问题在于我没有背景和拇指的颜色。我有实际的PNG文件。因此,PNG会被拉伸以适应开关的大小。 - Sunny
@Sunny 为什么你想要使用 PNG 文件呢?它比 Shape Drawable 占用更多的内存,而且很难维护焦点状态。如果你正在使用 Nine Patch,它不会起作用,应该使用 Shape。 - Asthme
我非常喜欢绘图。如果只是一个简单的形状,比如矩形,我可以画。但这里背景上有一些图标放在上面。我无法绘制这个图标,所以我不得不使用 PNG 图片。 - Sunny
@Sunny 然后计算 dp,并将 png 文件放置在所有 drawables -Xhdpi、hdpi 等中... - Asthme

4
final int switchWidth = Math.max(
    mSwitchMinWidth,
    2 * mThumbWidth + paddingLeft + paddingRight);

final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;

上面的代码是 Switch 类中的 onMeasure() 方法,设置 android:switchMinWidth 不能正确地改变宽度,唯一简单的方法是通过反射动态地改变 mSwitchWidth 字段。
try {
    Class clazz = Class.forName(Switch.class.getName());
    final Field switchWidth = clazz.getDeclaredField("mSwitchWidth");

    if (switchWidth != null) {
        switchWidth.setAccessible(true);        
        int width = widthWhateverYouWant;
        switchWidth.set(switchClassInstance, width);
        setMeasuredDimension(width, getMeasuredHeight());
        return;
    }
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}

不要忘记调用setMeasuredDimension(width, getMeasuredHeight());,因为Switch的measuredWidth并不是基于mSwitchWidth字段。

我猜你已经知道如何改变宽度,所以改变高度应该不难 :)


谢谢!我无法正确设置小于2倍拇指宽度的宽度~ - Zhming
这肯定有效。使用反射总是有点糊弄人,但我真的看不出还有更简单的方法来实现这个效果。 - Tharkius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接