Dans l'extrait de code ci-dessous, il y a une ligne commentée. Lorsque je décommenter cette ligne, le contenu de LinearLayout
ne sont pas affichés dans un TableRow
. Sans définir le LayoutParams
, la ligne affiche les deux textes. Je ne comprends pas ce comportement. Je sais que je peux inclure des vues complexes via xml
fichiers, mais je préfère comprendre ce qui ne va pas avec ce code:L'application de LayoutParams cache la vue
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// when I comment out this line, the row only shows the second text.
// linearLayout.setLayoutParams(layoutParams);
TextView textLabel = new TextView(this);
textLabel.setText("inside linear layout");
linearLayout.addView(textLabel);
TextView message = new TextView(this);
message.setText("inside tablerow");
tableRow.addView(linearLayout);
tableRow.addView(message);
tableLayout.addView(tableRow);
peut-être vous oubliez d'ajouter disposition params pour une vue de texte? Je pense que la largeur et la hauteur de TextView est maintenant 0dp, et linearLayout a des paramètres match_parent, donc à cause de cela, vous voyez une vue vide –
Quand j'utilise 'textLabel.setHeight (20); textLabel.setWidth (20) 'cela ne montre pas non plus – shaft
essayer de changer layoutParams source de linearLayout new LinearLayout.LayoutParams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); –