按钮的背景颜色在TableLayout

时间:2014-03-06 20:46:27

标签: android

我无法弄清楚为什么会这样做:

    TableLayout table = (TableLayout) this.findViewById(R.id.tablelayout);
    int counter = 1;
    for (int i = 0; i < nrows; i++) {
        TableRow row = new TableRow(this);
        table.addView(row);
        for (int j = 0; j < ncols; j++) {
            FrameLayout cell = new FrameLayout(this);
            cell.setBackgroundColor((i + j) % 2 == 0 ? Color.YELLOW : Color.WHITE);
            Button b = new Button(this, null, android.R.attr.buttonStyleSmall);
            b.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10);
            b.setText("B-\n" + Integer.toString(counter++));
            b.setPadding(0,0,0,0);
            b.setLayoutParams(new FrameLayout.LayoutParams(120, 120, 0x11));
                // 0x11 = center
            if (i == 3 && j == 3)
                b.setBackground(new ColorDrawable(0xcc0088));
            cell.addView(b);
            row.addView(cell);
            cell.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1F));
        }
        row.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1F));
    }

我正在使用nrows==10ncols==5。这显示了白色和黄色“正方形”的棋盘图案,并且除了(3,3)之外的每个正方形的中心是灰色按钮,其中包含一些文本。但是,在(3,3)方格中,我使用setBackgroundColorDrawable使用了一种洋红色,只显示了文字;除了文字,广场完全是黄色的。没有灰色按钮,但我尝试应用于按钮的洋红色也没有出现在任何地方。 (使用b.setBackgroundColor(0xcc0088)具有相同的结果。)

是否有正在发生的事情的解释?我错过了一些明显的东西,或setBackgroundTableLayout内的按钮有什么不同,或者是什么?如何以编程方式设置按钮的颜色,而不更改封闭方块的颜色?

0 个答案:

没有答案
相关问题