Android 动态生成多行多列控件

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.RelativeLayout;

public class TestTextActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout aLayout = (RelativeLayout) this
        .findViewById(R.id.relativeLayout1);
    RelativeLayout.LayoutParams relativeLayoutParams = null;
    int chk_id = 1000;
    CheckBox checkBox = null;

    int rowCount = 8; // 行总数
    int colCount = 4; // 列总数(这里不含第一列)

    for (int i = 0; i < rowCount; i++) { // 控制行
        checkBox = new CheckBox(this);
        checkBox.setId(chk_id += 10);
        relativeLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (0 == i) { // 如果是第一行第一列,单独处理
        relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        } else {
        relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT,
            chk_id - 10);
        relativeLayoutParams.addRule(RelativeLayout.BELOW, chk_id - 10);
        }
        checkBox.setText(String.valueOf(chk_id));
        checkBox.setLayoutParams(relativeLayoutParams);
        aLayout.addView(checkBox);
        // ******************
        for (int j = 1; j < colCount; j++) { // 控制列
        checkBox = new CheckBox(this);
        checkBox.setId(chk_id + j);
        checkBox.setText(String.valueOf(chk_id + j));
        relativeLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, chk_id
            + j - 1);
        relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP, chk_id
            + j - 1);
        checkBox.setLayoutParams(relativeLayoutParams);
        aLayout.addView(checkBox);
        }
    }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:weightSum="1">
    <RelativeLayout android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0.18">
    </RelativeLayout>    
</LinearLayout>

posted on 2012-05-25 17:11  厕所蹲个猴  阅读(845)  评论(0编辑  收藏  举报

导航