2017-04-19 69 views
0

我想膨脹包含幾個textViews的佈局。當我將它添加到tableRow中時,該佈局內的textView正在重疊。換句話說,layout_width和layout_height我被忽略了。充氣佈局的孩子將它們添加到TableRow時相互重疊

但是當我在充氣後添加rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));時,沒有任何東西出現。

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> 

回答

1

膨脹的佈局已經佈局PARAMS所有CHID觀點並不重新繪製新的佈局PARAMS。如果你想在膨脹後擁有與之前相同的視圖,你可以使用下面的代碼。 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)'這已經做到了。非常感謝。 –

0

相反的:

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

嘗試:

TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
      TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT); 
相關問題