【问题标题】:TextView FILL_PARENT doesn't workTextView FILL_PARENT 不起作用
【发布时间】:2015-12-11 20:40:42
【问题描述】:

我想让 TextView 填充父级,但我的代码不起作用。

这是我的代码:

TableLayout tableLayout = new TableLayout(this);

    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
    TableRow.LayoutParams param = new TableRow.LayoutParams();

    tableRowParams.setMargins(1, 1, 1, 1);
    param.span = 2;
    param.setMargins(1, 1, 1, 1);

    int trainIndex = 0;
    int stationIndex = 1;


    for(int i = 0; i < 6; i++){
        TableRow row = new TableRow(this);

        if(i == 0){
            TextView text = new TextView(this);

            text.setText("Way");
            text.setBackgroundColor(Color.GRAY);
            text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
            row.addView(text, tableRowParams);

            for(int j = 0; j < 4; j++) {
                TextView text1 = new TextView(this);

                text1.setText(getInfo(0, stationIndex));
                text1.setBackgroundColor(Color.GRAY);
                row.addView(text1, param);
                stationIndex++;
            }
        }else {

            for (int j = 0; j <= 8; j++) {
                TextView text = new TextView(this);


                if(j == 0){
                    text.setText(getInfo(1,trainIndex));
                    text.setBackgroundColor(Color.GRAY);
                    row.addView(text, tableRowParams);
                    trainIndex++;
                }else {
                    text.setText("   train  " + j);
                    text.setBackgroundColor(Color.GRAY);
                    row.addView(text, tableRowParams);
                }
            }
        }
        tableLayout.addView(row);
    }

    setContentView(tableLayout);

当我将布局参数应用于 textview 时,它不起作用,当文本不是很长时,它会导致单元格中出现空白,我希望所有单元格都有灰色背景。

这是它的外观示例:

我做错了什么?

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    确保您的容器具有FILL_PARENT 属性

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setStretchAllColumns(true);
    tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
    

    【讨论】:

      【解决方案2】:

      您忘记为 TextView 设置 LayoutParams。默认情况下,宽度和高度都使用 WRAP_CONTENT。

      另外,使用 MATCH_PARENT 而不是 FILL_PARENT

      编辑

      试试这个:

      TableRow row = new TableRow(this);
      TextView text = new TextView(this);
      text.setText("Way");
      text.setBackgroundColor(Color.GRAY);
      text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                          LayoutParams.WRAP_CONTENT));
      row.addView(text);
      

      【讨论】:

      • 对不起,实际上我想要我的 TextView match_parent。
      • 这就是问题所在。默认情况下,您的 TextView 不是 match_parent,并且您不要在代码中更改它。检查我的编辑。
      • 仍然没有,这是 XML 中的文本视图:
      • 您是否将 TableRow 添加到表格中?
      • 是的。我已经创建了表格布局,而不是表格行和行的文本视图,现在我想要那个文本视图 match_parent。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 2012-07-13
      相关资源
      最近更新 更多