2011-08-17 4 views
9

मेरे टॉगल-बटन में प्रत्येक राज्य (लाल और सफेद) के लिए अलग-अलग रंगीन पृष्ठभूमि हैं। अब सक्रिय होने पर मुझे टॉगलबटन-टेक्स्ट (लाल/सफेद) का रंग बदलने की जरूरत है। एक्सएमएल के साथ मैं इसे काम नहीं कर सकता, शायद किसी को पता है कि मैं क्या गलत कर रहा हूं?राज्य परिवर्तन पर एंड्रॉइड ToogleButton के टेक्स्ट रंग को कैसे बदलें?

लेआउट एक्सएमएल में मेरे बटन:

बटन-राज्यों के लिए xml:

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

और रंग के लिए xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- pressed --> 
<item android:state_pressed="true" android:color="#4f5459" /> 

<!-- focused --> 
<item android:state_focused="true" android:color="#4f5459" /> 

<!-- default --> 
<item android:color="#ffffff" /> 

<!-- trying these out, but none works --> 
<item android:state_checked="true" android:color="#ff0000" /> 
<item android:state_enabled="true" android:color="#ff00dd" /> 
<item android:state_selected="true" android:color="#ff00dd" /> 
<item android:state_active="true" android:color="#ff00dd" /> 

</selector> 

उत्तर

-1

आप उपयोग कुछ नीचे दिए गए उदाहरण है , आइटम टैग में निहित रंग विशेषताओं को स्थानांतरित करें।

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

<item android:state_pressed="true" > 
    <shape> 
     <corners 
      android:topLeftRadius="3dp" 
      android:bottomLeftRadius="0.1dp" 
      android:topRightRadius="0.1dp" 
      android:bottomRightRadius="3dp" /> 
     <gradient 
      android:startColor="#d0785e" 
      android:endColor="#000000" 
      android:angle="270" /> 
    </shape> 
</item> 

<item android:state_focused="true" > 
    <shape> 
     <corners 
      android:topLeftRadius="3dp" 
      android:bottomLeftRadius="0.1dp" 
      android:topRightRadius="0.1dp" 
      android:bottomRightRadius="3dp" /> 
     <gradient 
      android:endColor="#ffffff" 
      android:startColor="#b9b9b9" 
      android:angle="270" /> 
    </shape> 
</item> 

<item android:state_enabled="false"> 
    <shape> 
     <corners 
      android:topLeftRadius="3dp" 
      android:bottomLeftRadius="0.1dp" 
      android:topRightRadius="0.1dp" 
      android:bottomRightRadius="3dp" /> 
     <gradient 
      android:startColor="#f0aa9f" 
      android:endColor="#e21f00" 
      android:angle="270" /> 
    </shape> 
</item> 

<item>   
    <shape> 
     <corners 
      android:topLeftRadius="3dp" 
      android:bottomLeftRadius="0.1dp" 
      android:topRightRadius="0.1dp" 
      android:bottomRightRadius="3dp" /> 
     <gradient 
      android:startColor="#fe9c69" 
      android:endColor="#fc5700" 
      android:angle="270" /> 
    </shape> 
</item> 


</selector> 
+0

आपकी मदद माइक डी के लिए धन्यवाद, मैं याद है कि मैं का उपयोग करके अनियंत्रित/जाँच बटन राज्य प्राप्त कर सकते हैं "एंड्रॉयड: state_checked =" true "" और एंड्रॉयड: state_checked = "झूठा" ;-) – gue

15

यह मिला: अब मैं android:state_checked="true" और android:state_checked="false" उपयोग कर रहा हूँ।

रंग-xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:color="#ffffff" /> 
    <item android:state_checked="false" android:color="#000000" /> 
</selector> 
+1

नोट: xml फ़ाइल को 'रंग' res फ़ोल्डर में होना चाहिए –

संबंधित मुद्दे