2016-10-06 4 views
0

Je tente de placer la barre de défilement (verticale et horizontale) pour la disposition de la table.Barre de défilement pour TableLayout

Voici mon xml:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="40" 
    android:fillViewport="true"> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true"> 

     <TableLayout 
      android:id="@+id/prize_table" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:padding="10dp" 
      android:shrinkColumns="*" 
      android:stretchColumns="*" /> 
    </HorizontalScrollView> 
</ScrollView> 

Voilà comment j'ajouter la ligne du tableau:

// header 
row = new TableRow(MainActivity.this); 
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

tv = new TextView(MainActivity.this); 
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
tv.setTextSize(18); 
tv.setPadding(0, 5, 0, 5); 
tv.setText(R.string.prize_no); 
row.addView(tv); 

tv = new TextView(MainActivity.this); 
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
tv.setTextSize(18); 
tv.setPadding(0, 5, 0, 5); 
tv.setText(R.string.prize_name); 
row.addView(tv); 

layout.addView(row); 

for (Prize prize : prizes) { 
    row = new TableRow(MainActivity.this); 
    row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    tv = new TextView(MainActivity.this); 
    tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    tv.setTextSize(18); 
    tv.setPadding(0, 5, 0, 5); 
    tv.setText(prize.getPrizeID()); 
    row.addView(tv); 

    tv = new TextView(MainActivity.this); 
    tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    tv.setTextSize(18); 
    tv.setPadding(0, 5, 0, 5); 
    tv.setText(prize.getPrizeName()); 
    row.addView(tv); 

    layout.addView(row); 
} 

Mais j'ai un problème avec ces codes

  1. La barre de défilement verticale ne peut correspondre à mon contenu, disons que j'ai 10 lignes, mais la barre de défilement sera 10 fois plus longue
  2. La barre de défilement horizontale est manquante

Quelqu'un pourrait-il indiquer quel problème dans mon code? Merci à l'avance

Répondre

0

Après plusieurs essais, j'ai découvert que le problème était le

android:shrinkColumns="*" 

Après enlever que, à la fois problème a été résolu
Espérons que cela peut aider d'autres personnes avec le même problème.