我正在编写棋盘。对于布局,我包括几个“合并”布局。我只是想从 Activity 代码中访问“合并的”组件,以更改它们的一些属性(imageView background_color、imageView 图像路径 ...)。
编辑:例如,我想传递一个行 id (R.id.row_1),并使用 for 循环从该行 id 初始化所有组件:为 8 个单元格赋予每个不同的背景颜色。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeComponents(R.id.row_2);
}
public initializeComponents(int id){
/* inflates the given layout child components
* and set their properties
*/
}
这是我的 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/board"/>
</LinearLayout>
这是我的 board.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include android:id="@+id/row_8" layout="@layout/board_line"/>
<include android:id="@+id/row_7" layout="@layout/board_line"/>
<include android:id="@+id/row_6" layout="@layout/board_line"/>
<include android:id="@+id/row_5" layout="@layout/board_line"/>
<include android:id="@+id/row_4" layout="@layout/board_line"/>
<include android:id="@+id/row_3" layout="@layout/board_line"/>
<include android:id="@+id/row_2" layout="@layout/board_line"/>
<include android:id="@+id/row_1" layout="@layout/board_line"/>
</TableLayout>
这是我的 board_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
<ImageView
android:id="@+id/h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/cell_dimension"
android:minHeight="@dimen/cell_dimension"
android:maxWidth="@dimen/cell_dimension"
android:maxHeight="@dimen/cell_dimension"
/>
</TableRow>
提前感谢您的帮助。