我试图设计一个水平滚动的简单应用程序,其中填充了图像按钮。
我能够填充列表,但前提是我手动有6个按钮。我有没有办法用数组和循环来做到这一点?使用btn,btn1,btn2等感觉很笨拙。
我的linearList被引用为mLinearList
我还没有设置onClickListeners。
private void fillPaintingGallery() {
ImageButton btn = new ImageButton(this);
btn.setImageDrawable(getResources().getDrawable(R.drawable.painting1));
mLinearList.addView(btn);
ImageButton btn1 = new ImageButton(this);
btn1.setImageDrawable(getResources().getDrawable(R.drawable.painting2));
btn1.setContentDescription(RenaissanceDatabase.description[1]);
mLinearList.addView(btn1);
ImageButton btn2 = new ImageButton(this);
btn2.setImageDrawable(getResources().getDrawable(R.drawable.painting3));
btn2.setContentDescription(RenaissanceDatabase.description[2]);
mLinearList.addView(btn2);
ImageButton btn3 = new ImageButton(this);
btn3.setImageDrawable(getResources().getDrawable(R.drawable.painting4));
btn3.setContentDescription(RenaissanceDatabase.description[3]);
mLinearList.addView(btn3);
ImageButton btn4 = new ImageButton(this);
btn4.setImageDrawable(getResources().getDrawable(R.drawable.painting5));
btn4.setContentDescription(RenaissanceDatabase.description[4]);
mLinearList.addView(btn4);
ImageButton btn5 = new ImageButton(this);
btn5.setImageDrawable(getResources().getDrawable(R.drawable.painting6));
btn5.setContentDescription(RenaissanceDatabase.description[5]);
mLinearList.addView(btn5);
}
谢谢
答案 0 :(得分:1)
为什么不将Grid View或Listview用于您的要求
for (int i = 0; i < RenaissanceDatabase.description[].length; i++) {
ImageButton btn = new ImageButton(this);
int id = getResources().getIdentifier("yourpackagename:drawable/painting" + (i+1), null, null);
btn.setImageDrawable(getResources().getDrawable(
id));
btn.setContentDescription(RenaissanceDatabase.description[i]);
mLinearList.addView(btn);
}
答案 1 :(得分:0)
&#39; Android&#39;使用ListView和适配器执行所需操作的方法。 ListView是保存项目列表的容器。适配器控制显示的内容及其显示方式。
以下是填充列表的good example。您可以修改示例,以便模板视图包含ImageButton,模型将具有图像uri和描述。