好的,这是我用于在父LinearLayout中使用RelativeLayouts绘制单个配方步骤列表的方法。 RelativeLayouts在Icon旁边包含一个TextView,它们都应该随每个i ++而改变。我发现它没有任何问题,没有错误或异常,结果只是没有显示。
这是最好的方法吗?有没有更好的办法?我应该学习如何使用ListView吗?它更容易吗?任何帮助表示赞赏。
stepsList.setOrientation(LinearLayout.VERTICAL);
for (int i=0; i==arrayLength; i++) {
RelativeLayout stepsBlock = new RelativeLayout(getApplicationContext());
ImageView recipeIcon = new ImageView(getApplicationContext());
TextView recipeText = new TextView(getApplicationContext());
RelativeLayout.LayoutParams blockParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams iconParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
blockParams.setMargins(0, 2, 0, 0);
if (i > 0) {
stepsBlock.setLayoutParams(blockParams);
}
stepsBlock.setPadding(3, 3, 3, 3);
stepsBlock.setBackgroundColor(Color.parseColor("#b69878"));
iconParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iconParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
iconParams.setMargins(0, 0, 6, 0);
recipeIcon.setBackgroundResource((com.package.app.R.drawable.iconwhite)+(i));
recipeIcon.setLayoutParams(iconParams);
recipeIcon.setId(1);
textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textParams.addRule(RelativeLayout.RIGHT_OF, recipeIcon.getId());
recipeText.setPadding(3, 3, 3, 3);
recipeText.setText(recipeTinDough[i]);
recipeText.setTextSize(12);
recipeText.setTextColor(Color.BLACK);
recipeText.setLayoutParams(textParams);
recipeText.setId(2);
stepsList.addView(stepsBlock);
stepsBlock.addView(recipeIcon);
stepsBlock.addView(recipeText);
}
}
答案 0 :(得分:0)
我强烈建议您使用ListView实现这一整个过程,原因如下:
但如果您不想使用ListView:
setPadding
和setMargins
使用像素而不是倾角,设置3像素填充似乎很奇怪(边距相同)recipeIcon.setBackgroundResource((com.package.app.R.drawable.iconwhite)+(i));
错误,您将资源ID添加为1,android很可能无法找到您正在寻找的资源。您可能还必须使stepsList
与父级匹配。我无法用这么多代码告诉你。