我在java中使用以下代码在RelativeLayout中添加一些自定义视图,并使用 addRule 将其与先前添加的视图右侧对齐,但是在第一次迭代时添加的视图现在显示(其余的意见似乎很好。)
RelativeLayout.LayoutParams params;
int pegIds[] = {Peg.BLUE, Peg.GREEN, Peg.RED, Peg.WHITE, Peg.YELLOW, Peg.PURPLE};
Peg peg;
for (int i = 0; i < pegIds.length; i++) {
peg = new Peg(context, null);
peg.setId(i);
peg.setTag("hotbar_" + i);
peg.setDrawableID(pegIds[i]);
params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0)
params.addRule(RelativeLayout.RIGHT_OF, i-1);
hotbar.addView(peg, params);
}
RelativeLayout XML:
<RelativeLayout android:id="@+id/hotbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp" ></RelativeLayout>
答案 0 :(得分:0)
我不认为视图的Id值为0
试
for (int i = 1000; i < 1000 + pegIds.length; i++) {