【问题标题】:Custom switch appears huge using custom drawables使用自定义可绘制对象,自定义开关看起来很大
【发布时间】:2018-01-12 15:06:16
【问题描述】:

我正在使用 3 个可绘制文件制作自定义 switch。两个图标,OnOff,由 89x89 图像表示,以及一个自定义的 Track 可绘制资源(黑色),它是 171x93。当我将自定义可绘制对象设置为开关时,它会拉伸到图像的大小。如何缩小这些图像以完全适合并且不大于文本本身?


这是我的switchxml:

<Switch
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:id="@+id/switchAnimation"
        android:thumb="@drawable/my_switch"
        android:switchMinWidth="55dp"
        android:track="@drawable/toggle_track"
        android:textOff=""
        android:textOn=""
        android:textColor="#FFF"/>


开关my_switch.xml的drawable:

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


轨道的drawable:

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

我已经在两个模拟器设备上进行了测试,Galaxy nexus API 21 和 Nexus 4 API 19。另一个问题,我还注意到,点击 API 19 设备中的开关后,没有像在API 21(而不是图标只是出现和消失),这正常吗?!

![nenx​​us 4 API 19 没有自定义轨道][2]


编辑
我将资源缩小了很多,为 25x25,现在看起来大小合适,但看起来有点像素化。有没有办法将质量图标缩小到更小的尺寸?

【问题讨论】:

    标签: android xml toggle toggleswitch


    【解决方案1】:

    只需添加零透明度的描边:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:state_enabled="true">
            <shape android:shape="oval" android:visible="true">
                <solid android:color="@android:color/white" />
                <size android:width="20dp" android:height="20dp" />
                <stroke android:width="7dp" android:color="#00000000" />
            </shape>
        </item>
        <item android:state_checked="false" android:state_enabled="false">
            <shape android:shape="oval" android:visible="true">
                <solid android:color="#ccc" />
                <size android:width="20dp" android:height="20dp" />
                <stroke android:width="7dp" android:color="#00000000" />
            </shape>
        </item>
    </selector>
    

    Sample Image

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多