将图像与Android中的单选按钮相关联

时间:2011-11-23 14:30:33

标签: java android radio-button

我想从我的URL中获取图像并将其添加到进一步添加到广播组的单选按钮。但是,我找不到像setText这样的metod来设置单选按钮的文本。我希望以类似的方式将图像添加到我的单选按钮。是否有任何提示/方法可以为我做同样的事情?任何提示/代码示例都对我有用,

        RadioGroup rg = new RadioGroup (this); 
            rg = (RadioGroup) findViewById(R.id.radioGroup1);
            RadioButton rb[]= new RadioButton[children.size()];

            for (int i = 0; i < var.size(); i++) 
                {   
                Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);                                            
                MovieName[i]=movieAtt.getAttributeValue( "Title" );
                MovieCover[i]=movieAtt.getAttributeValue( "cover" );
                ShowTime[i]=movieAtt.getAttributeValue( "showtime" );

                rb[i] = new RadioButton(this);
                rb[i].setText(movieAtt.getAttributeValue("Title"));
                rg.addView(rb[i]);  

                //Calling this func to get Images
                //LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));
                //rb[i].buildDrawingCache(LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" )));



public static Drawable LoadImageFromWebOperations(String url) 
{
        try 
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } 
        catch (Exception e) 
        {
            return null;
        }
}

1 个答案:

答案 0 :(得分:1)

图像或文字有问题吗?至于图像,我相信这应该有效:

Drawable d = LoadImageFromWebOperations(movieAtt.getAttributeValue("cover"));
rb[i].setButtonDrawable(d);

确保将重力设置为Gravity.CENTER,否则您的图像将不会以单选按钮为中心。

您应该在RadioButton reference中找到有关该文本的信息。