2017-04-19 2 views
0

Je suis en train de gonfler une mise en page qui contient quelques textViews. Quand j'ajoute que à un tablerow, les textViews à l'intérieur que la mise en page sont se chevauchent. En d'autres termes, le layout_width et layout_height sont ignorées, je suppose.les enfants de mise en page qui se chevauchent Gonflé les uns avec les autres lorsque les ajoutant à une TableRow

Mais quand j'ajoute rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); après le gonflage, rien apparaît.

TableRow tableRow = new TableRow(this); 
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT); 
tableRow.setLayoutParams(lp); 

View rowView = getLayoutInflater().inflate(R.layout.layout_result_row, null); 
rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
loadResultRow(rowView, result); //This method sets the text of the textViews 
tableRow.addView(rowView); 
tableLayout.addView(tableRow); 

layout_result_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/results_row_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingBottom="16dp"> 

    <TextView 
     android:id="@+id/results_row_match_no" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_turn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/results_row_match_no" 
     android:layout_alignParentEnd="true" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_teams" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toEndOf="@+id/results_row_match_no" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_turn_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/results_row_turn" 
     android:layout_toStartOf="@+id/results_row_turn" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_toss_ml" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/results_row_match_no" 
     android:layout_marginTop="22dp" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_home_ml" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/results_row_toss_ml" 
     android:layout_alignBottom="@+id/results_row_toss_ml" 
     android:layout_centerHorizontal="true" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_rank_ml" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/results_row_home_ml" 
     android:layout_toEndOf="@+id/results_row_turn_text" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/results_row_result" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/results_row_turn_text" 
     android:layout_alignStart="@+id/results_row_home_ml" 
     android:text="TextView" /> 
</RelativeLayout> 

Répondre

1

La mise en page a déjà gonflé la mise en page Params toutes les vues ne sont pas chid redessiner avec la nouvelle mise en page params. Si vous voulez avoir les mêmes vues qu'avant après avoir gonflé, vous pouvez utiliser le code ci-dessous. enter image description here

TableLayout tableLayout = new TableLayout(getApplicationContext()); 
    tableLayout.setBackgroundColor(Color.RED); 
    TableRow tableRow = new TableRow(this); 
    TableRow.LayoutParams lp = new 
    TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT); 
    tableRow.setLayoutParams(lp); 

    View rowView = getLayoutInflater().inflate(R.layout.activity_main, null); 
    tableLayout.setStretchAllColumns(true); 
    // rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    // loadResultRow(rowView, result); //This method sets the text of the textViews 
    tableRow.addView(rowView); 
    tableLayout.addView(tableRow); 
    setContentView(tableLayout); 
+0

'tableLayout.setStretchAllColumns (true)' cela a fait l'affaire. Merci beaucoup. –

0

Au lieu de:

TableRow.LayoutParams lp = new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT); 

Essayez:

TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
      TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);