更改切换按钮颜色

Joh*_*ohn 1 android listview colors togglebutton

如何更改listView中toggleButton的颜色?

这就是我如何设计我的 toggleButton

<ToggleButton
            android:id="@+id/donePic"
            android:layout_width="30dp"
            android:layout_marginLeft="270dp"
            android:layout_height="30dp"
            android:background="@drawable/selector"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOff=""
            android:textOn=""/>
Run Code Online (Sandbox Code Playgroud)

选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use tic -->
    <item android:drawable="@mipmap/done"
        android:state_checked="true" >
        <solid
            android:color="@color/red" />
    </item>
    <!-- When not selected, use un tic-->
    <item android:drawable="@mipmap/done"
        android:state_checked="false">
        <solid
            android:color="@color/green" />
    </item>

</selector>
Run Code Online (Sandbox Code Playgroud)

我的活动

public View getView(int position, View convertView, ViewGroup parent) { // inside adapter class
        ViewHolder holder;   
        ToggleButton toggle;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_to_do, null);
            toggle =(ToggleButton)convertView.findViewById(R.id.donePic);
            toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        Log.e("A","a");
                    } else {
                        // The toggle is disabled
                    }
                }
            });
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        return convertView;
    }
Run Code Online (Sandbox Code Playgroud)

单击log时显示toggleButton,但颜色仍然保持白色(其原始颜色)。我怎样才能改变 的颜色toggleButton

更新

我已经更新了我的代码,但颜色仍然是白色的!我想使用done.png并将其放置到res/drawable/done.png.

选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use tic -->
    <item android:drawable="@drawable/toggle_on"
        android:state_checked="true" >
    </item>
    <!-- When not selected, use un tic-->
    <item android:drawable="@drawable/toggle_off"
        android:state_checked="false">
    </item>

</selector>
Run Code Online (Sandbox Code Playgroud)

切换打开

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/done"
        android:state_checked="true">
        <solid
            android:color="@color/green" />
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

切换_关闭

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/done"
        android:state_checked="false">
        <solid
            android:color="@color/red" />
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

V-r*_*hit 5

试试这个方法。

切换按钮:

<ToggleButton 
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:textOn="On"
    android:textOff="Off"
    android:textSize="20sp"
    android:background="@drawable/toggle_day_bg_selector" />
Run Code Online (Sandbox Code Playgroud)

toggle_day_bg_selector.xml

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

切换_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"
    >
    <solid android:color="@color/red" />

</shape>
Run Code Online (Sandbox Code Playgroud)

切换_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid android:color="@color/green" />

</shape>
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助。

编辑 :

使用这个可绘制文件来显示图像 ToggleButton

切换_off.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_green_dark" />
        </shape>
    </item>
    <item android:drawable="@drawable/ic_launcher">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_green_dark" />
        </shape>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

切换_on.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_red_dark" />
        </shape>
    </item>
    <item android:drawable="@drawable/ic_launcher">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_red_dark" />
        </shape>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

快乐编码..