【问题标题】:Center TextView Programmatically on TableRow以编程方式在 TableRow 上居中 TextView
【发布时间】:2013-04-19 16:50:38
【问题描述】:

我正在尝试将 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);

    }

提前致谢;)

【问题讨论】:

    标签: android textview center tablelayout tablerow


    【解决方案1】:

    终于成功了! 这是解决方案:

    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);
        }
    

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多