繁体   English   中英

如何更改 Switch Widget 的大小

[英]How to change the size of a Switch Widget

在 Ice Cream Sandwich 中,引入了一个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" />

轨迹和拇指可绘制对象是九个补丁图像,应缩放到所有可能的大小。 我希望 Switch 能够在给定范围内缩放到最大尺寸,但似乎可绘制对象只是在提供的空间内居中。

是否可以增加 Switch 的大小以使其看起来更大?

使用 scale 属性更改大小对我有用。

将这些行添加到 xml 文件中的<switch/>标记。

android:scaleX="2"
android:scaleY="2"

您可以根据需要更改比例值。 这里的值 2 使它的大小加倍,类似的值 0.5 使它的大小减半。

例子:

       <Switch
            android:id="@+id/switchOnOff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleX="2"
            android:scaleY="2"/>

我想我终于明白了这一点:

  1. 切换字幕大小由<switch android:textSize=""... />
  2. 切换拇指文本大小由<switch android:switchTextAppearance="".../> 打造属于自己的风格。 这种样式很重要,因为它控制拇指的整体宽度(稍后会详细介绍)。
  3. 整体开关高度由<switch android: track ="@drawable/shape_mythumb".../> 您使用了九个补丁,我怀疑这就是您遇到问题的原因。 我使用了<shape android:shape="rectangle"...></shape>并且成功了。
  4. 拇指可绘制的高度<switch android:thumb="".../>被调整以匹配轨道的高度。 拇指可绘制对象的高度仅对拇指的形状很重要。 它不会影响开关的高度。

所以这是我发现的:

  1. 对开关拇指和开关轨道使用<shape>...</shape>可绘制对象。
  2. 两个可绘制对象的宽度无关紧要。 我将我的设置为“0dp”
  3. 轨道的高度决定了开关的整体高度。
  4. 最长的android:textOffandroid:textOn将决定拇指的宽度。 这决定了整个开关的宽度。 开关宽度始终是拇指宽度的两倍。
  5. 如果拇指文本宽度小于轨道的高度,拇指形状将被缩小。 这会扭曲拇指的形状。 如果发生这种情况,请添加空格以增加“关闭”或“开启”文本的宽度,直到它至少与您的轨道高度一样宽。

这是足够的信息,至少可以开始设计您想要的大小和形状的开关。 如果你想出其他任何事情,请告诉我。

上面的答案是正确的。 这是一个如何使用 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>

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>

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>

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>

最后在开关

采用

android:switchMinWidth="56dp"
android:thumb="@drawable/thumb"
android:track="@drawable/track"

我今天处理了一个类似的问题,我发现从 Jelly Bean 开始你可以使用setSwitchMinWidth(width); 设置完整开关的最小宽度。 但是,我还认为,如果您的文本太大,则小部件会在左侧部分被切断。 使用setThumbTextPadding(num);更改拇指上的默认文本填充在这里可能会派上用场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);
}

不幸的是 - 无论出于何种愚蠢的原因 - 开关小部件都没有对此做出反应。 我试图在OnLayoutChangeListener做到这一点,但OnLayoutChangeListener没有成功。 理想情况下,在该状态下也应该知道开关的当前大小,我想像这样将可用空间“分配”到填充:

int textPadding = Math.max(0, (sw.getWidth() - containerWidth) / 4);
sw.setThumbTextPadding(textPadding);

但是sw.getWidth()onSizeChanged() sw.getWidth()仍然返回 0 ,可能是因为它仍然没有正确布局。 是的,我们快到了……如果你偶然发现了什么,请告诉我 :)

轨道的高度不必在视觉上确定拇指/开关的整体高度。 我向我的轨道可绘制形状添加了一个透明的笔触,这会导致轨道周围的填充:

<?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>

使用 SwitchCompat 代替 Switch 和 scaleX、sxaleY,padding 效果更好

<androidx.appcompat.widget.SwitchCompat
                android:id="@+id/switch_transport"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:scaleX="1.5"
                android:scaleY="1.5"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM