TableRow.LayoutParams buttonLayout = new TableRow.LayoutParams(100, 100); //无效
buttonLayout.weight = 1;
buttonLayout.gravity = Gravity.CENTER_VERTICAL;
buttonLayout.setMargins(5, 5, 5, 5); final View button = renderButton(mToolBar.buttons.get(index), context); TableRow row = new TableRow(context);
        row.addView(button, buttonLayout);
TableRow的document说“The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.”在仍然使用TableRow作为父控件的情况下(但不一定仍然使用TableRow.LayoutParams),有办法设置button的大小吗?

解决方案 »

  1.   

    先要有个下面的类public class TableAdapter extends BaseAdapter {
    private Context context;
    private List<TableRow> table; public TableAdapter(Context context, List<TableRow> table) {
    this.context = context;
    this.table = table;
    } @Override
    public int getCount() {
    return table.size();
    } @Override
    public long getItemId(int position) {
    return position;
    } public TableRow getItem(int position) {
    return table.get(position);
    } public View getView(int position, View convertView, ViewGroup parent) {
    TableRow tableRow = table.get(position);
    return new TableRowView(this.context, tableRow);
    } /**
     * TableRowView 实现表格行的样式
     * 
     * @author hellogv
     */
    class TableRowView extends LinearLayout {
    public TableRowView(Context context, TableRow tableRow) {
    super(context); this.setOrientation(LinearLayout.HORIZONTAL);
    for (int i = 0; i < tableRow.getSize(); i++) {// 逐个格单元添加到行
    TableCell tableCell = tableRow.getCellValue(i);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
    tableCell.width, tableCell.height);// 按照格单元指定的大小设置空间
    layoutParams.setMargins(0, 0, 1, 1);// 预留空隙制造边框
    if (tableCell.type == TableCell.STRING) {// 如果格单元是文本内容
    TextView textCell = new TextView(context);
    textCell.setLines(1);
    textCell.setGravity(Gravity.CENTER);
    textCell.setBackgroundColor(Color.BLACK);// 背景黑色
    textCell.setTextSize(16);
    textCell.setText(String.valueOf(tableCell.value));
    addView(textCell, layoutParams);
    } else if (tableCell.type == TableCell.IMAGE) {// 如果格单元是图像内容
    ImageView imgCell = new ImageView(context);
    imgCell.setBackgroundColor(Color.BLACK);// 背景黑色
    imgCell.setImageResource((Integer) tableCell.value);
    addView(imgCell, layoutParams);
    }
    }
    this.setBackgroundColor(Color.WHITE);// 背景白色,利用空隙来实现边框
    }
    } /**
     * TableRow 实现表格的行
     * 
     * @author hellogv
     */
    static public class TableRow {
    private TableCell[] cell; public TableRow(TableCell[] cell) {
    this.cell = cell;
    } public int getSize() {
    return cell.length;
    } public TableCell getCellValue(int index) {
    if (index >= cell.length)
    return null;
    return cell[index];
    }
    } /**
     * TableCell 实现表格的格单元
     * 
     * @author hellogv
     */
    static public class TableCell {
    static public final int STRING = 0;
    static public final int IMAGE = 1;
    public Object value;
    public int width;
    public int height;
    private int type; public TableCell(Object value, int width, int height, int type) {
    this.value = value;
    this.width = width;
    this.height = height;
    this.type = type;
    }
    }
    }然后listview.setAdapter(getTable());private TableAdapter getTable() {
    ArrayList<TableRow> table = new ArrayList<TableRow>();
    TableCell[] titles = new TableCell[6];// 每行6个单元
    int width = a.getWindowManager().getDefaultDisplay().getWidth()
    / titles.length;
                     //这里可以设置每个列的宽度
    titles[0] = new TableCell("1", width + 8 * 3,
    LayoutParams.FILL_PARENT, TableCell.STRING);
    titles[1] = new TableCell("2", width + 8 * 16,
    LayoutParams.FILL_PARENT, TableCell.STRING);
    titles[2] = new TableCell("3", width + 8 * 2,
    LayoutParams.FILL_PARENT, TableCell.STRING);
    titles[3] = new TableCell("4", width + 8 * 2,
    LayoutParams.FILL_PARENT, TableCell.STRING);
    titles[4] = new TableCell("5", width + 8 * 2,
    LayoutParams.FILL_PARENT, TableCell.STRING);
    titles[5] = new TableCell("6", width + 8 * 2,
    LayoutParams.FILL_PARENT, TableCell.STRING);
    table.add(new TableRow(titles));
    TableAdapter tableAdapter = new TableAdapter(a, table);
    return tableAdapter;
    }