简体   繁体   中英

Dynamic Span Adjustment for TextView In TableLayout

Is there a way that I can set the span of a textview object in the java code, instead of having to do it in xml? I tried someone else's solution of using LayoutParams . I don't know if i didn't implement it correctly or if its just the wrong solution or maybe im just not realizing what my mistake in the code is, but it's causing the application to crash. Any help would be appreciated, im posting my code below.

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


}

}

With the aid of the code at this link , I have accomplish this issue like that

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

while creating rows dynamically at java code side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM