如何在Android GridView中动态添加带图像和文本的按钮?

时间:2011-07-13 06:25:07

标签: android image gridview dynamic button

我想将ImageButtons动态文字添加到GridView(如下所示),并设置listener,以便用户点击其中一个不同的页面图标。按钮需要先水平填充,所以我想我需要2个循环。我已经搜索过,但我找不到一个如何做到这一点的好教程。

Image buttons in GridView

我打算怎么做

ArrayList<String> options = getOptions();

for(int i=0; i<options.size(); i++){
    String opt = options.get(i);
    if(opt.equals("Contacts")){
        //Add Contacts button with image in GridView
        Button btn = new Button(this); 
        //Set button text
        //Set image
        //Set listener to go to another activity
        layout.addView(btn); 
    }else if(..){
        //Add another button with image in GridView
        //Set button text
        //Set image
        //Set listener to go to another activity
    }

}

如果有人可以指导我如何在ButtonImage中动态设置Button TextlistenerGridView,我将非常感激。提前谢谢!

1 个答案:

答案 0 :(得分:0)

 - List item Create xml file and take Gridview in it and even Textview 
reference it in the class/java file you wnat to use 
 - List item
Now when you use the grid view you will use the imageadapter  and extends  the base adapter 
while in that you will have the getview() function where u set the image source dynamically and even set the Textview every time 

我找到了一个例子,请你参考

http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/