在TableRow中以编程方式居中TextView

3
我会很乐意为您翻译相关的IT技术内容。以下是需要翻译的内容:

我想要把一个TextView居中放置在一个TableRow里面,而这个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);

    }

感谢您的提前帮助;)
1个回答

6
最终让它运作起来了!以下是解决方案:
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);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接