2011-07-04 28 views
0

有没有一种方法可以在java代码中设置textview对象的范围,而不必在xml中执行?我尝试了别人的解决方案,使用LayoutParams。我不知道如果我没有正确实现它,或者如果它只是错误的解决方案,或者即时通讯只是没有意识到我的代码中的错误,但它导致应用程序崩溃。任何帮助将不胜感激,即时发布我的代码如下。在TableLayout中TextView的动态范围调整

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() 
{ 


} 

}

回答

0

的代码有了这个link的帮助下,我同时在Java代码中端动态创建行完成这个问题就像

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