0

我正在编写棋盘。对于布局,我包括几个“合并”布局。我只是想从 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>

提前感谢您的帮助。

4

1 回答 1

0

您可以使用 LayoutInflater(或完全以编程方式创建视图)并将所有这些组合到您的需要。以下示例应创建一些随机颜色的 5x5 板。由于在代码中设置 LayoutParams 很难看,我会在 xml 中设置它们并膨胀布局。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
    TableLayout tl = (TableLayout) ll.findViewById(R.id.tableLayout);
    for (int j = 0; j < 5; j++) {
        TableRow tr = (TableRow) inflater.inflate(R.layout.table_row, null);
        for (int i = 0; i < 5; i++) {
            ImageView iv = (ImageView) inflater.inflate(R.layout.image_view, null);
            iv.setBackgroundColor(new Random().nextInt());
            tr.addView(iv);
        }
        tl.addView(tr);
    }
    setContentView(ll);
}

主要的.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TableLayout>
</LinearLayout>

table_row.xml

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TableRow>

image_view.xml

just one of those you defined in board_line.xml:
于 2012-02-16T19:43:18.123 回答