【问题标题】:Android Switch thumb image stretchedAndroid Switch拇指图像拉伸
【发布时间】:2023-03-16 12:55:01
【问题描述】:

我需要在 Android 应用程序中自定义 Switch。 我正在使用此代码:

<Switch
    android:id="@+id/switchmode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true" 
    android:textOn=""
    android:textOff=""
    android:switchMinWidth="0dp"
    android:thumb="@drawable/switch_bg"
    android:track="@drawable/track_bg"
    android:layout_marginBottom="20dp" 
    android:layout_marginRight="20dp" />

track_bg 文件是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item                               android:drawable="@drawable/switch_track" />
</selector>

switch_bg 文件是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/switch_enable" />
    <item android:state_pressed="true"  android:drawable="@drawable/switch_press" />
    <item android:state_checked="true"  android:drawable="@drawable/switch_check_on" />
    <item                               android:drawable="@drawable/switch_enable" />
</selector>
  • switch_check_on 为 32 x 31
  • switch_enable 为 32 x 31
  • switch_press 为 32 x 31
  • switch_track 为 64 x 31

结果是一个拉伸的图像(它应该是一个圆形但它被压扁了):

https://www.dropbox.com/s/07iigspuh3dpfnh/switchstreched.jpg

有什么明显的错误吗?

【问题讨论】:

    标签: android layout android-switch stretched


    【解决方案1】:

    创建9-patch 图像并指定Android 应如何拉伸它。你可以阅读更多here

    【讨论】:

    • 应该修补什么?轨道文件或拇指文件?
    • 两者。打开图像 9-patch 编辑器并检查 Android 将如何拉伸它。拉伸不取决于您的开关工作逻辑,而是 - android 设备分辨率和密度。因此,如果您有 1 个适用于所有分辨率和密度的图像资源 - 将其设为 9-patch,它将按指定规则进行拉伸。
    • 看看在这个示例项目中是如何实现的github.com/hearbeathorror/Switch-Compat-Changing-Colors/tree/…
    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2015-06-15
    • 2015-06-17
    相关资源
    最近更新 更多