有没有办法使开关轨迹比拇指更长?

时间:2019-08-04 16:50:13

标签: android android-layout

我正在尝试显示以下输出:

enter image description here

我的问题是比拇指长

我已经有一个较细的音轨,但是拇指在音轨的尽头:

enter image description here

enter image description here

这是我的XML:

我在布局中自定义的开关元素:

<Switch
    android:id="@+id/invite_member_switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="55dp"
    android:thumb="@drawable/thumb_background"
    android:track="@drawable/track_background" />

我的@drawable/thumb_background。只是一个选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_thumb_checked" android:state_checked="true" />
    <item android:drawable="@drawable/switch_thumb_normal" android:state_checked="false" />
</selector>

我的@drawable/switch_thumb_checked。具有所需颜色的圆形元素:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="20dp"
        android:height="20dp" />
    <solid
        android:color="@color/violet_light" />
</shape>

我的@drawable/switch_thumb_normal。与以前的圆形元素一样,具有我想要的颜色:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="20dp"
        android:height="20dp" />
    <solid
        android:color="@color/pink_light" />
</shape>

我的@drawable/track_background。我发现解决方案的路径更短:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/violet_light_50" />
    <size android:height="5dp" />
    <corners android:radius="40dp" />
    <!-- results in the track looking smaller than the thumb -->
    <stroke
        android:width="8dp"
        android:color="#00ffffff" />
</shape>

1 个答案:

答案 0 :(得分:0)

switch_thumb_checkedswitch_thumb_normal的代码更改为

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
  <item>
      <shape android:shape="oval"
          >
          <padding android:top="5dp"
              android:left="5dp"
              android:right="5dp"
              android:bottom="5dp"/>
          <solid android:color="@android:color/transparent"/>
          <size android:width="30dp"
              android:height="30dp"/>
      </shape>
  </item>
    <item
        >

        <shape android:shape="oval">
            <size android:width="20dp"
                android:height="20dp"/>
            <solid android:color="@color/violet_light"/>
        </shape>
    </item>
</layer-list>