【问题标题】:How to change height and width of Switch in Android如何在Android中更改Switch的高度和宽度
【发布时间】:2014-01-12 18:58:27
【问题描述】:
  1. 我希望将开关安装在 120dp 宽度和 30dp 高度。
  2. 此外,我希望拇指中没有文字,但希望拇指覆盖总宽度的一半,因此我希望它为 60dp。
  3. 另外,我希望拇指的高度略小于可以从 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 的问题。

谢谢

【问题讨论】:

标签: android switch-statement


【解决方案1】:

添加此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"

【讨论】:

  • 但这只会确保 minWidth 不低于 56dp。我希望它是 120dp。那么这有什么帮助呢?
  • 我做到了,但关键是我的图像大于 120 dp 并且 hieght 大于 10 dp。但我想分别将其修复为 120 和 10
  • 祝你好运……我也遇到了同样的问题
  • 无法正确设置小于 2x thumb 的宽度
【解决方案2】:

这里是一个小例子,如何改变你的 swicth 宽度, 我希望它会帮助一些人..

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 为什么要使用 png 文件..它比形状 drawables 需要更多内存。而且它有点难以保持焦点状态...如果你使用九个补丁,它不会工作,改用形状
  • 我很喜欢画画。如果它只是一个像矩形这样的简单形状,我可以。但是这里的背景有一个放置在它上面的图标。我无法绘制图标。所以我不得不使用 png。
  • @Sunny 然后计算dp,并将png文件放入所有drawables -Xhdpi,hdpi.etc...
【解决方案3】:
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

我想改变高度并不难,因为你知道如何改变宽度:)

【讨论】:

  • 谢谢!我无法正确设置小于 2x thumbs 的宽度~
  • 这绝对有效。使用反射总是有点笨拙,但我真的找不到更简单的方法来实现这种效果。
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多