简体   繁体   English

Android自定义开关被剪裁/切断

[英]Android custom Switch gets clipped/cut off

I've a custom android switch which is define like this 我有一个自定义的android开关,它定义如下

<Switch
    android:id="@+id/create_site_switch_ssl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|left"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:checked="true"
    android:gravity="center_vertical"
    android:thumb="@drawable/btn_gradient" />

and the thumb selector item like this 以及像这样的拇指选择器项目

<item android:state_enabled="true" android:state_focused="false" android:state_pressed="false">
    <shape android:shape="rectangle">    
        <gradient
            android:startColor="@color/Blue100"
            android:endColor="@color/Blue50"
            android:angle="90"/>    
        <padding android:left="@dimen/button_padding"
            android:top="@dimen/button_padding"
            android:right="@dimen/button_padding"
            android:bottom="@dimen/button_padding" />    
        <corners android:radius="@dimen/button_corner"/>    
        <stroke android:color="@color/BlueBorder" android:width="5dp"/>    
    </shape> 
</item>

On Android version 4.3, 4.4.2 switch slider gets cut like this on the right and left sides. 在Android版本4.3,4.4.2开关滑块在左右两侧被切割成这样。

在此输入图像描述

I tried to play around with the shape's and switch's padding/margin/size nothing worked so far. 我试着玩形状和开关的填充/边距/尺寸到目前为止没有任何效果。

Also setting a custom track with appropriate paddings didn't work. 同时使用适当的填充设置自定义轨道也不起作用。

On Android 5.0 and higher versions the problem was solved by setting 在Android 5.0及更高版本上,问题已通过设置解决

android:paddingLeft 
android:paddingRight

Why the padding is not working for later versions ? 为什么填充不适用于更高版本? What I'm missing ? 我错过了什么? Any hint for the solution is welcomed ! 欢迎任何解决方案的提示!

I had the same issue. 我遇到过同样的问题。 Removing the padding properties from the drawable fixed it for me. 从drawable中删除padding属性为我修复了它。 In your case, it would look like this: 在你的情况下,它看起来像这样:

<item android:state_enabled="true" android:state_focused="false" android:state_pressed="false">
    <shape android:shape="rectangle">    
        <gradient
            android:startColor="@color/Blue100"
            android:endColor="@color/Blue50"
            android:angle="90"/>    
        <corners android:radius="@dimen/button_corner"/>    
        <stroke android:color="@color/BlueBorder" android:width="5dp"/>    
    </shape> 
</item>

You can then add the padding to the switch: 然后,您可以将填充添加到交换机:

<Switch
    android:id="@+id/create_site_switch_ssl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|left"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:checked="true"
    android:drawablePadding="@dimen/button_padding"
    android:gravity="center_vertical"
    android:thumb="@drawable/btn_gradient" />

I know this is late but this is for other people who have the same issue. 我知道这已经晚了,但这适用于其他有同样问题的人。 For me the solution was to set a negative padding. 对我来说,解决方案是设置负填充。 So if your switch is cut off on the right side then set right padding to a negative number such as -5 or -10 depending on how much of the switch is being cut off. 因此,如果您的开关在右侧被切断,则将右侧填充设置为负数,例如-5或-10,具体取决于切换的切换器的数量。

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

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