Android 以编程方式动态添加imagebutton

Android 以编程方式动态添加imagebutton,android,Android,我得到了字符串数组的动态长度,我想在imagebutton中显示图像,在水平视图中也显示图像,就像我添加了按钮一样,但它显示在垂直布局中。这是我的代码: for (int i =0;i<adapt_objmenu.image_array.length;i++){ ImageButton b1 = new ImageButton(myrefmenu); b1.setId(100 + i); // b1.setText(adapt_objmenu.cit

我得到了字符串数组的动态长度,我想在imagebutton中显示图像,在水平视图中也显示图像,就像我添加了按钮一样,但它显示在垂直布局中。这是我的代码:

for (int i =0;i<adapt_objmenu.image_array.length;i++){
    ImageButton b1 = new ImageButton(myrefmenu);
        b1.setId(100 + i);

       // b1.setText(adapt_objmenu.city_name_array[i]);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (i > 0) {
            lp.addRule(RelativeLayout.BELOW, b1.getId() - 1);
        }   
        b1.setLayoutParams(lp);
        relative.addView(b1);

    //relate.addView(b1, i,  new RelativeLayout.LayoutParams(width,height));

    //height = height+80;
}
for(int i=0;i 0){
lp.addRule(RelativeLayout.down,b1.getId()-1);
}   
b1.设置布局参数(lp);
相对视图(b1);
//relate.addView(b1,i,新RelativeLayout.LayoutParams(宽度、高度));
//高度=高度+80;
}

在设置图像按钮的第三行中使用此选项

  for (int i =0;i<adapt_objmenu.image_array.length;i++){
     ImageButton b1 = new ImageButton(myrefmenu);
        b1.setId(100 + i);
         b1.setImageResource(R.drawable.imagename);
       // b1.setText(adapt_objmenu.city_name_array[i]);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (i > 0) {
            lp.addRule(RelativeLayout.RIGHT_OF, b1.getId() - 1);
        }   
        b1.setLayoutParams(lp);
        relative.addView(b1);

//relate.addView(b1, i,  new RelativeLayout.LayoutParams(width,height));

//height = height+80;



}
for(int i=0;i 0){
lp.addRule(RelativeLayout.RIGHT_OF,b1.getId()-1);
}   
b1.设置布局参数(lp);
相对视图(b1);
//relate.addView(b1,i,新RelativeLayout.LayoutParams(宽度、高度));
//高度=高度+80;
}

您确实意识到您在下面设置了
RelativeLayout.
,而
RelativeLayout.RIGHT\u的
将更有意义,如果我正确理解你的问题?好的,我明白了,但是如何在imagebutton上设置图像这是主要问题你的意思是你想在添加到
RelativeLayout
后设置图像,用于
imagebutton
。在代码的第三行之后,我想将位图图像添加到imagebutton,但不知道如何请帮助Bad,坏主意。。。为什么不改用
ListView
“ListView”针对这类内容进行了优化…实际上,我想将一个静态图像设置为imagebutton的图像,因为我想将其显示为分页控件,就像顶部的白点一样,以显示指示页面数的数组长度。您的图像位于Drawable文件夹或其他位置。如果位于Drawable文件夹中,则可以使用
mImageButton.setImageResource(R.drawable.ic_启动器)然后您应该准备图像资源Id数组,并将其用于循环中的应用。