2014-06-08 4 views
0

J'ai besoin de mon écran pour que deux vues de texte soient définies dans la mise en page de l'activité. Au-dessous de ces vues, je veux définir une table dont le nombre de lignes et de colonnes dépendra de l'entrée de l'utilisateur (dynamique).Configuration dynamique de la table via Java de manière dynamique

Ce code est dans le oncreate de l'activité

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home_page); 

    TableLayout tableLayout = new TableLayout(getApplicationContext()); 
    TableRow tableRow; 
    TextView textView; 

    for (int i = 0; i < no_days; i++) { 
     tableRow = new TableRow(getApplicationContext()); 
     for (int j = 0; j < no_subjects; j++) { 
      textView = new TextView(getApplicationContext()); 
      textView.setText("Hello"); 
      textView.setPadding(20, 20, 20, 20); 
      tableRow.addView(textView); 
     } 
     tableLayout.addView(tableRow); 
    } 

    setContentView(tableLayout); 

Mais ce code est de prendre tout l'écran et je ne peux pas avoir les vues de texte que je définis dans la mise en page d'activité.

Voici mon activity_home_page.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.attendancerecord.HomePage" 
tools:ignore="MergeRootFrame" > 

<TextView 
    android:id="@+id/textView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="10dp" 
    android:text="@string/welcome" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/display_user_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView5" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="33dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 
+0

Déposez votre activity_home_page – Manishika

+0

vous dire xml fichier ? –

+0

Oui activity_home_page.xml – Manishika

Répondre

1

Si vous souhaitez que votre TableLayout au-dessous votre TextViews puis utilisez LinearLayout avec l'orientation "vertical "dans activity_home_page au lieu de RelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.attendancerecord.HomePage" 
tools:ignore="MergeRootFrame" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:layout_marginTop="10dp" 
    android:text="welcome" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/display_user_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="33dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

Et ajouter mainLinear.addView(tableLayout); à la fin de la boucle

LinearLayout mainLinear = (LinearLayout) findViewById(R.id.container); 

     for (int i = 0; i < 5; i++) { 
      tableRow = new TableRow(getApplicationContext()); 
      for (int j = 0; j < 4; j++) { 
       textView = new TextView(getApplicationContext()); 
       textView.setText("Hello"); 
       textView.setPadding(20, 20, 20, 20); 
       tableRow.addView(textView); 
      } 
      tableLayout.addView(tableRow); 
     } 
     mainLinear.addView(tableLayout); 

Et enlever setContentView(tableLayout);

espère que cela fonctionne

0

Ajouter ceci à votre fichier d'activité:

<TableLayout 
    android:id="@+id/table1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignTop="@+id/display_user_name" 
    android:layout_marginTop="15dp" > 

</TableLayout> 

Et dans votre classe:

TableLayout tableLayout = (TableLayout)findViewById(R.id.table1); 
     TableRow tableRow; 
     TextView textView; 
      for (int i = 0; i < no_days; i++) { 
       tableRow = new TableRow(getApplicationContext()); 
       for (int j = 0; j < no_subjects; j++) { 
        textView = new TextView(getApplicationContext()); 
        textView.setText("Hello"); 
        textView.setPadding(20, 20, 20, 20); 
        tableRow.addView(textView); 
       } 
       tableLayout.addView(tableRow); 
      }