如何在Android中更改Switch的高度和宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20762490/
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 height and width of Switch in Android
提问by Sunny
- I woudld like to fit the switch in 120dp width and 30dp height.
- Also I want to no text in the thumb but would like to have the thumb cover half of the total width so i want it to be 60dp.
- Also, I want the height of the thumb to be little less than the height of the switch of the background could be seen from 3 sides.
- 我想将开关安装在 120dp 宽度和 30dp 高度。
- 此外,我希望拇指中没有文字,但希望拇指覆盖总宽度的一半,所以我希望它是 60dp。
- 另外,我希望拇指的高度略小于从 3 个侧面可以看到的背景开关的高度。
I have no idea how to do #3 and for #1 and #2, I tried the following xml, but it is not working:
我不知道如何做 #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" />
could someone please help me with #1, #2 and #3.
有人可以帮我 #1、#2 和 #3。
Thanks
谢谢
回答by Nambi
add this android:switchMinWidth="56dp"
and try
添加这个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"
回答by Asthme
here is a small example how to change your swicth width, i hope it will help some one..
这是一个如何改变你的开关宽度的小例子,我希望它会帮助一些人..
1)Use ur color for thumb (color_thumb.xml)
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 color for track (gray_track.xml)
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)Selector for thumb (thumb.xml)
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) Selector for track (track.xml)
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" />
and finally in switch
最后在开关
use
用
android:switchMinWidth="56dp"
android:thumb="@drawable/thumb"
android:track="@drawable/track"
回答by 1step2hell
final int switchWidth = Math.max(
mSwitchMinWidth,
2 * mThumbWidth + paddingLeft + paddingRight);
final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;
The code above is method onMeasure()
in class Switch, set android:switchMinWidth
can not change the width properly,
the only easy way is that change field mSwitchWidth
by reflection dynamically:
上面的代码是onMeasure()
类Switch中的方法,setandroid: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();
}
Do not forget setMeasuredDimension(width, getMeasuredHeight());
,
because measuredWidth
of Switch is not base on field mSwitchWidth
.
不要忘记setMeasuredDimension(width, getMeasuredHeight());
,因为measuredWidth
Switch 不是基于领域的mSwitchWidth
。
I guess it's not difficult to change height since you know how to change width :)
我想改变高度并不难,因为你知道如何改变宽度:)