这是我的布局文件:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imageLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/silver">
<TextView
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignBaseline="@id/imageLayout"
android:layout_alignBottom="@id/imageLayout"
android:background="@color/red"
android:gravity="center"
android:textColor="@color/white"
android:textSize="30sp"
/>
<TextView
android:id="@+id/above"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingBottom="3dp"
android:layout_above="@id/bottom"
android:background="@color/blue"
android:gravity="center"
android:textColor="@color/white"
android:textSize="30sp"
</RelativeLayout>
我正在使用以下代码将 ImageViews 动态添加到名为 MatchGame.xml 的 about 布局中
RelativeLayout re = (RelativeLayout)findViewById(R.id.imageLayout);
re.setGravity(Gravity.CENTER);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
final ImageView img = new ImageView(this);
//do some operations with image
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(40,40);
if (j == 0) {
// rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
// RelativeLayout.TRUE);
}
else {
rlp.addRule(RelativeLayout.RIGHT_OF, index - 1);
}
if (i == 0) {
// rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,
// RelativeLayout.TRUE);
}
else {
rlp.addRule(RelativeLayout.BELOW, index - cols);
}
img.setLayoutParams(rlp);
re.addView(img);
}
}
图像以矩阵的形式添加,但视图与屏幕的左侧对齐,但我希望矩阵位于屏幕的中心。