How to change Width of Android's Switch track?

后端 未结 4 1893
醉梦人生
醉梦人生 2020-12-29 05:34

I\'m going crazy with this tiny problem. I have a \'switch\' Form widget, but no matter how much I try, I cannot make it narrower. Even if I have one character instead of \'

相关标签:
4条回答
  • 2020-12-29 05:44

    Here is my solution. I removed the texts, set the padding of the track and define the switchMinWidth properties. This is my xml:

    <Switch
        android:id="@+id/theSwitchId"
        android:textOn=""
        android:textOff=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:thumbTextPadding="5dp"
        android:switchMinWidth="40dp"
        android:layout_gravity="right|center_vertical" />
    
    0 讨论(0)
  • 2020-12-29 05:45

    You should use android:track instead of android:thumb.

    All my code:

    <Switch
        android:id="@+id/switch_security_tog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:background="@color/transparent"
        android:button="@null"
        android:textOff=""
        android:textOn=""
        android:thumb="@null"
        android:switchMinWidth="56dp"
        android:track="@drawable/thumb" />
    
    0 讨论(0)
  • 2020-12-29 05:59

    Who are using androidx.appcompat.widget.SwitchCompat need to use

    app:switchMinWidth
    

    Example:

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:switchMinWidth="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:track="@drawable/custom_track"
        android:thumb="@drawable/custom_thumb"
       />
    
    0 讨论(0)
  • 2020-12-29 06:04

    Set your desired width of switch in the attribute:

    android:switchMinWidth
    

    for example:

    <Switch
        android:id="@+id/switchVisitAgain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="1dp"
        android:checked="false"
        android:gravity="center_vertical"
        android:switchMinWidth="56dp"
        android:textOff=""
        android:textOn=""
        android:thumb="@drawable/round_button"
        android:track="@drawable/button_black" />
    
    0 讨论(0)
提交回复
热议问题