且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改toggleButton颜色

更新时间:2022-10-15 23:29:47

b

ToggleButton:

 < 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/>

toggle_day_bg_selector.xml

 <?xml version =1.0encoding =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>

toggle_on.xml

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

< / shape>

toggle_off.xml

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

< / shape>

希望这将有所帮助。



strong> EDIT:



使用此可绘制文件显示 ToggleButton



toggle_off.xml

 <?xml version = 1.0encoding =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>

toggle_on.xml

 <?xml version =1.0encoding =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>

快乐编码..


How to change the color of toggleButton in listView?

This is how I deigned my 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=""/>

selector

<?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>

MyActivity

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;
    }

It displays log when toggleButton is clicked, but the color still remain white(its original color). How can I change the color of toggleButton ?

Updated

I have updated my code, but the color still remains white ! I want to use the done.png and I have placed it to res/drawable/done.png.

selector

<?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>

toggle_on

<?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>

toggle_off

<?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>

try this way.

ToggleButton :

<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" />

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>

toggle_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>

toggle_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>

Hope this will help.

EDIT :

use this drawable files for showing images on ToggleButton

toggle_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>

toggle_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>

happy coding..