Jan*_*usz 52 android android-switch
在冰淇淋三明治中,引入了一个Switch Widget,它显示了On Off Slider.
我像这样添加了Switch:
<Switch
android:layout_width="fill_parent"
android:layout_height="48dp"
android:textStyle="bold"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_bg_selector" />
Run Code Online (Sandbox Code Playgroud)
轨道和拇指绘图是九个补丁图像,可以扩展到所有可能的大小.我希望Switch可以扩展到给定边界内的最大大小,但似乎drawable只是在提供的空间内居中.
是否可以增加Switch的大小以使其看起来更大?
Qua*_*ium 92
我想我终于搞清楚了:
<switch android:textSize=""... /><switch android:switchTextAppearance="".../>.烹饪自己的风格.这种风格很重要,因为它控制了拇指的整体宽度(稍后会详细介绍).<switch android:track="@drawable/shape_mythumb".../><shape android:shape="rectangle"...></shape><switch android:thumb="".../>调整拇指抽屉的高度以匹配轨道的高度.拇指的可绘制高度仅对拇指的形状很重要.它不会影响开关的高度.所以这就是我发现的:
<shape>...</shape>drawables用于开关拇指和开关轨道.android:textOff或android:textOn将决定拇指的宽度.这决定了整个开关的宽度.开关宽度始终是拇指宽度的两倍.这是足够的信息,至少可以开始设计您想要的尺寸和形状的开关.如果你弄清楚其他什么,请告诉我.
Gau*_*rav 78
使用scale属性来改变大小对我有用.
将这些行添加到<switch/>xml文件中的标记中.
android:scaleX="2"
android:scaleY="2"
Run Code Online (Sandbox Code Playgroud)
您可以根据需要更改比例值.这里的值2使它的大小加倍,类似的值0.5使它的大小减半.
例:
<Switch
android:id="@+id/switchOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="2"
android:scaleY="2"/>
Run Code Online (Sandbox Code Playgroud)
Ast*_*hme 44
上面的答案是对的.这里有一个小例子如何使用shape drawable改变你的开关宽度我希望它能帮助一些人..
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"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
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"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
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" />
</selector>
Run Code Online (Sandbox Code Playgroud)
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" />
</selector>
Run Code Online (Sandbox Code Playgroud)
最后在交换机中
使用
android:switchMinWidth="56dp"
android:thumb="@drawable/thumb"
android:track="@drawable/track"
Run Code Online (Sandbox Code Playgroud)
我今天处理了类似的问题,我发现从Jelly Bean开始你可以setSwitchMinWidth(width);用来设置整个开关的最小宽度.我还想到,如果你的文字太大,那么小部件就会在左边部分被切掉.更改拇指上的默认文本填充可能会派上用场, setThumbTextPadding(num);您可以修改它.
唯一的问题 - 实际上这是一个拦截器 - 到目前为止我偶然发现当然应该根据父容器的大小扩展交换机.为此我将开关包装在自定义线性布局中(这也为API <= 15提供了后备)并尝试了这个:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (android.os.Build.VERSION.SDK_INT >= 16) {
adaptSwitchWidth(w);
}
}
@TargetApi(16)
private void adaptSwitchWidth(int containerWidth) {
Switch sw = (Switch) compoundButton;
sw.setSwitchMinWidth(containerWidth);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是 - 出于任何愚蠢的原因 - 交换机小部件对此没有反应.我试图在OnLayoutChangeListener没有成功的情况下做到这一点.理想情况下,交换机的当前大小也应该在该状态下已知,我想将自由空间"分配"到填充,如下所示:
int textPadding = Math.max(0, (sw.getWidth() - containerWidth) / 4);
sw.setThumbTextPadding(textPadding);
Run Code Online (Sandbox Code Playgroud)
但sw.getWidth()仍然返回0 onSizeChanged(),可能是因为它仍然没有正确布局.是的,我们差不多......如果你偶然发现了什么,请告诉我:)
小智 7
轨道的高度不必在视觉上确定拇指/开关的整体高度.我在我的轨道可绘制形状上添加了一个透明笔划,导致在轨道周围填充:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/track_color"/>
<size android:height="@dimen/track_height" />
<size android:width="@dimen/track_width" />
<!-- results in the track looking smaller than the thumb -->
<stroke
android:width="6dp"
android:color="#00ffffff"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71218 次 |
| 最近记录: |