2

我正在尝试将 textview 居中在 tablerow 内,这本身就是在 tablelayout 内,但我想我遗漏了一些东西,因为 TextView 没有居中:s

这是我的方法:

private void insertClock() {


        TableLayout table = new TableLayout(this);
        table.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        table.setStretchAllColumns(true);

        TableRow tbRow = new TableRow(this);
        TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);




        clock = new TextView(this);
        TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);  


        clock.setLayoutParams(layoutHistory);
        clock.setText(calculateClockText());
        tbRow.setLayoutParams(layoutRow);
        table.addView(tbRow);
        clock.setGravity(Gravity.CENTER_HORIZONTAL);
        tbRow.addView(clock);

    }

提前致谢 ;)

4

1 回答 1

6

Finnaly 成功了!这是解决方案:

private void insertClock(TableLayout table, String text) {

        TableRow tbRow = new TableRow(this);
        TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);


        tbRow.setLayoutParams(layoutRow);

        clock = new TextView(this);
        TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);  
        clock.setLayoutParams(layoutHistory);
        clock.setText(text);


        clock.setGravity(Gravity.CENTER);
        tbRow.setGravity(Gravity.CENTER);
        tbRow.addView(clock);
        table.addView(tbRow);
    }
于 2013-04-25T17:47:15.340 回答