TableLayout中TextView的动态跨度调整

时间:2011-07-04 04:59:27

标签: java android xml textview html

有没有办法可以在java代码中设置textview对象的范围,而不必在xml中进行?我尝试了其他人使用 LayoutParams 的解决方案。我不知道我是否没有正确实现它,或者它只是错误的解决方案,或者我只是没有意识到代码中的错误,但它导致应用程序崩溃。任何帮助将不胜感激,我发布下面的代码。

package com.szymon.atrium;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;


public class TapeChart extends Activity
{
private ArrayList<Room> rooms;
private ArrayList<Arrival> arrivals;
private GregorianCalendar startDate = new GregorianCalendar(2011,1,1);
private GregorianCalendar endDate = new GregorianCalendar(2011,1,10);
private ArrivalGrid grid = new ArrivalGrid(arrivals, rooms, startDate.getTime(), endDate.getTime());

private ArrayList<ArrayList<Cell>> displayArrival = grid.displayArrivalGrid();
private ArrayList<ArrayList<Cell>> displayRooms = grid.displayRoomGrid();

@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.tape);

    TableLayout tbl = (TableLayout)findViewById(R.id.tapeChart);

    for(ArrayList<Cell> a : displayRooms)
    {
        TableRow newRow = new TableRow(this);

        for(Cell c : a)
        {
            LayoutParams param = new LayoutParams();
            param.span=c.getColSpan();

            MyTextView cell = new MyTextView(this);
            cell.setText(c.getContent());
            cell.setLayoutParams(param);

            newRow.addView(cell);
        }

        tbl.addView(newRow);
    }
}

public TableLayout getArrivalsTableLayout()
{

    return null;
}

public void getRoomTableLayout()
{


}

}

1 个答案:

答案 0 :(得分:0)

借助此link的代码,我完成了这个问题

 `TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
  rowSpanLayout.span = 3;
  TextView textView = new TextView(this);
  textView.setText("Text Title");
  tableRow.addView(textView, rowSpanLayout);` 

在java代码端动态创建行。