好的,这是我用来在父 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);
}
}