I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:

<item android:state_checked="true" android:state_pressed="false"

      android:drawable="@color/app_tint"/>

with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:

android1:background="@drawable/radio_state"

OR

android1:button="@drawable/radio_state"

I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.

有帮助吗?

解决方案

Here's one we did for an app:

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

Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top