简体   繁体   中英

My radio button background is transparent, how do I make it opaque?

Update 1:

I followed the directions provided by Archimedes below. Problem is that my API 9 "drawable" folder already has an XML doc titled "btn_radio.xml" and the code inside is exactly the same as you presented above. Now, when I go back to eclipse and tell it:

`<RadioButton android:background="@drawable/btn_radio" ... />`

I get an error message in return saying Error: No resource found that matches the given name (at 'background' with value '@drawable/btn_radio_on').

So what I did instead is I went to the API 8 folder and looked found the drawable_hdpi and mdpi folders, did a search for btn_radio_off and btn_radio_on and copied those two images into the corresponding API 9 folders. This fixed the issue in Eclipse of radio button backgrounds not showing up. However, when I run my code, the virtual device still displays the old broken (transparent) image.*

Original Post:

Hi, I'm following a Gingerbread 2.3 (I'm running Win7/Eclipse) tutorial from this site and I'm looking at this image here: http://www.vogella.de/articles/Android/images/first50.gif

Each time I add a new radio button to my graphical layout in Main.xml the silver part of the radio button is transparent. So regardless of the background color of the main window, I will only see the outline of a radio button and that radio button will be filled in with the color of the entire background.

If I create a grouped item and put two radio buttons in a group, I can get toggle between the two and the green indicator showing which one is toggled is available but the buttons themselves are transparent.

How do I make my radio button look like the one in the image above? It seems that no matter how many times I start a "new" project, the problem persists. The only thing that affects it is toggling another "theme" but that's not really a solution once I run the app.

Thanks for your help.

This appears to be a new Gingerbread (2.3, API level 9) graphical style for the RadioButton drawable's constituent pngs.

The easiest fix is simply to not use Gingerbread... go to your project properties, select the "Android" tab, and change the "Project Build Target" to API Level 8 or lower.

This is a workaround I believe should work for you:

1). Find 'btn_radio_off.png', 'btn_radio_off_pressed.png', 'btn_radio_off_selected.png', 'btn_radio_on.png', 'btn_radio_on_pressed.png', 'btn_radio_on_selected.png' images at 2.2 sources. You can find them in your SDK subfolders. Note, there are several sets of the images (hdpi, mdpi, etc.). Recreate the file structure related to those files inside of your 'drawable' directory.

2). In your 'drawable' directory create the 'btn_radio.xml' file with the following content:

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

    <item android:state_checked="true" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_radio_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_radio_off_selected" />

    <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

This is actually the same state list that OS uses for radio buttons.

3). Then inside of your layout xml declare your custom radio button using the above state list drawable by using android:background attribute:

<RadioButton android:background="@drawable/btn_radio" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM