本文介绍了Android开关-开启/关闭开关背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我如何在我的应用程序中实现这样的开关吗?

does someone know how I can implement a switch like this in my application?

或者如何在打开/关闭时更改标准开关的背景色?

or how I change the backgroundcolor of standard switch on switching on/off?

推荐答案

以下是示例XML,供您开始:

Here is sample XML for you to start off with:

<Switch
    android:layout_width="75dp"
    android:layout_height="25dp"
    android:id="@+id/switch1"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:track="@drawable/switch_track_selector"
    android:switchMinWidth="75dp"
    android:thumb="@drawable/switch_thumb_selector" />

switch_track_selector.xml

switch_track_selector.xml

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

    <item
        android:drawable="@drawable/switch_track_off"
        android:state_checked="false"/>

</selector>

switch_track_on.xml

switch_track_on.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:radius="15dp" />
    <size
        android:width="75dp"
        android:height="25dp" />
    <solid
        android:color="#3E98F3" />
    <stroke
        android:width="1dp"
        android:color="#000000" />
</shape>

switch_track_off.xml

switch_track_off.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:radius="15dp" />
    <size
        android:width="75dp"
        android:height="25dp" />
    <solid
        android:color="#FFFFFF" />
    <stroke
        android:width="1dp"
        android:color="#000000" />
</shape>

switch_thumb_selector.xml

switch_thumb_selector.xml

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

    <item
        android:drawable="@drawable/switch_thumb"
        android:state_checked="false"/>

</selector>

switch_thumb.xml

switch_thumb.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <size
        android:width="25dp"
        android:height="25dp" />

    <solid
        android:color="#CCCCCC" />

    <stroke
        android:width="1dp"
        android:color="#000000" />

</shape>

这篇关于Android开关-开启/关闭开关背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:00