2013-11-18 4 views
1

Я новичок и сталкиваюсь с трудностями для достижения требуемого результата. XML КОД: // этот код находится внутри ScrollViewКак поместить динамический TableRow в TableLayout и разместить TextView динамически в TableRow с помощью Android?

<LinearLayout 
     android:id="@+id/statusSecond_Layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TableLayout 
      android:id="@+id/statusDisciplineTable_layout" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 

      ></TableLayout> 

    </LinearLayout> 

JAVA CODE:

setContentView(R.layout.status_view); 
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout); 
for(int i=0;i<2;i++) 
{ 
    TableRow statusTableRow = new TableRow(this); 
    statusTableRow.setId(i); 
    statusTableRow.setOrientation(TableRow.VERTICAL); 
    TextView productsTextView = new TextView(this); 
    productsTextView.setText("product name:"+i); 
    statusTableRow.addView(productsTextView); 
    //statusTableRow.setBackgroundColor(Color.BLACK); 


    for(int j=0;j<2;j++) 
    { 
     RelativeLayout statusRelativelayout = new RelativeLayout(this); 
     TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); 
     RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); 
     rl.setMargins(0, 0, 16, 0); 
     rl.addRule(RelativeLayout.ALIGN_LEFT); 
      TextView label = new TextView(this); 
     label.setId(j); 
     label.setText("abcd:"+j); 
     label.setLayoutParams(rl); 
    statusRelativelayout.addView(label); 
    statusTableRow.addView(statusRelativelayout,rlp); 

    } 
    statusTableLayout.addView(statusTableRow);} 

code output

пожалуйста, скажите мне, что я должен нужно сделать изменения в моем текущем коде требуемый продукт required outout

+0

, в которых размер экрана вы тестировали приложение? – krishna

+0

i m используя SAMSUNG S3, но эти снимки обрезаны и изменяются только из-за того, что сообщение в SO – nida

+0

почему вы не используете Linringayout вместо Tablerow, который может предоставить вам желаемый результат? – krishna

ответ

1

Вы можете использовать LinearLayout вместо TableRow как этот

TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout); 
     for(int i=0;i<2;i++) 
     { 
      LinearLayout statusTableRow = new LinearLayout(this); 
      statusTableRow.setId(i); 
      statusTableRow.setOrientation(LinearLayout.VERTICAL); 
      TextView productsTextView = new TextView(this); 
      productsTextView.setText("product name:"+i); 
      statusTableRow.addView(productsTextView); 
      //statusTableRow.setBackgroundColor(Color.BLACK); 


      for(int j=0;j<2;j++) 
      { 
       RelativeLayout statusRelativelayout = new RelativeLayout(this); 

       RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); 
       rl.setMargins(0, 0, 16, 0); 
       rl.addRule(RelativeLayout.ALIGN_LEFT); 
        TextView label = new TextView(this); 
       label.setId(j); 
       label.setText("abcd:"+j); 
       label.setLayoutParams(rl); 
      statusRelativelayout.addView(label); 
      statusTableRow.addView(statusRelativelayout); 

      } 
      statusTableLayout.addView(statusTableRow);} 

также поместить все внутри Relativelayout, а затем положить внутрь ScrollView как этот

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
<LinearLayout 
     android:id="@+id/statusSecond_Layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TableLayout 
      android:id="@+id/statusDisciplineTable_layout" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 

      > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 
      </LinearLayout> 

     </TableLayout> 

    </LinearLayout> 

</RelativeLayout> 

</ScrollView> 
+0

см. Этот ЛОК: ЭТО ПРАВО? TableRow.LayoutParams rlp = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); – nida

+0

да это правильно .. – krishna

+0

TableRow.LayoutParams rlp = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); --------------- изменились с этим LOC своим рабочим штрафом --------------------------- -------------------------------------- LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams (LinearLayout. LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); – nida

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