我有一个 2-dim。ImageViews 数组。它们以编程方式设置为 RelativeLayout。但是当我启动应用程序时,它们根本不会出现。这是我的代码:
private void setMap(ImageView[][] fields){
RelativeLayout relLay = (RelativeLayout) findViewById(R.id.screen);
OnClickListener listener = new MyOnClickListener();
for (int i = 0; i < numFieldsHeight; i++){
for (int j = 0; j < numFieldsWidth; j++){
ImageView img = new ImageView(MapActivity.this);
img.setImageResource(R.drawable.map);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (j != 0)
params.addRule(RelativeLayout.RIGHT_OF, fields[i][j-1].getId());
else
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
if (i != 0)
params.addRule(RelativeLayout.BELOW, fields[i-1][j].getId());
else
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.height = fieldLength;
params.width = fieldLength;
img.setVisibility(View.VISIBLE);
img.setId(getFieldId(j,i));
img.setOnClickListener(listener);
fields[i][j] = img;
relLay.addView(fields[i][j], params);
}
}
}
和
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >
<RelativeLayout
android:id="@+id/relLayDiffSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
<RelativeLayout
android:id="@+id/screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#222222" >
</RelativeLayout>
</RelativeLayout>
RelativeLayout“屏幕”的更多属性在代码中设置。但这行得通,我可以看到RelativeLayout,只是缺少ImageViews。
没有抛出异常。请帮我!
------!编辑 ! - - -
很抱歉,这段代码确实有效。问题是我忘记将变量 fieldLength 设置为不同于 0 的值。无论如何,非常感谢您的帮助!:)