2010-09-30 2 views
3

Я хочу динамически добавлять строки в таблицу. Это мой код.Динамическое создание Android-планшета

TableRow tableRow = null; 
TextView textView = null; 
ImageView imageView = null; 
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout); 
RelativeLayout relativeLayout = null; 
     for (String string: listOfStrings) { 
          tableRow = new TableRow(this); 
          relativeLayout = (RelativeLayout) View.inflate(this, 
            R.layout.row, null); 
          textView = (TextView) relativeLayout.getChildAt(0); 
          textView.setText(string); 
          imageView= (ImageView) relativeLayout.getChildAt(1); 
          imageView.setBackgroundColor(R.color.blue); 
          tableRow.addView(relativeLayout); 
          tableLayout.addView(tableRow); 
         } 

Я создал макет строки с шириной fill_parent, с TextView по крайней левой стороне и на правой ImageView большей стороне. Однако, когда я запускаю эту программу, ширина строки появляется wrap_content вместо fill_parent с текстом, перекрывающим изображение. Пожалуйста помоги. Спасибо

ответ

1

Первое, что я замечаю, это то, что вы не устанавливаете LayoutParams при добавлении своих просмотров.

TableRow tableRow = null; 
TextView textView = null; 
ImageView imageView = null; 
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout); 
RelativeLayout relativeLayout = null; 

for (String string: listOfStrings) 
{ 
    tableRow = new TableRow(this); 
    relativeLayout = (RelativeLayout) View.inflate(this, R.layout.row, null); 
    textView = (TextView) relativeLayout.getChildAt(0); 
    textView.setText(string); 
    imageView= (ImageView) relativeLayout.getChildAt(1); 
    imageView.setBackgroundColor(R.color.blue); 

    //Here's where I would add the parameters... 
    TableLayout.LayoutParams rlParams = new TableLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT); 
    tableRow.addView(relativeLayout, rlParams); 
    tableLayout.addView(tableRow); 
} 

На практике я бы также отвлекся от создания строк в их собственном методе. для упрощения использования/повторного использования.

Смежные вопросы